2014-04-01 4 views
0

예를 들어 5 개의 게시물을 포함하는 일부 카테고리가 있습니다. 게시물을 선택할 때 나머지 카테고리에있는 4 개의 게시물을 표시해야합니다.single.php에이 single.php가있는 카테고리의 모든 게시물을 표시하는 방법

<?php 
$categories = get_the_category($post->ID); 
if ($categories) { 
$category_ids = array(); 
foreach($categories as $individual_category) $category_ids[] = $individual_category- >term_id; 
$args=array(
'category__in' => $category_ids, 
'post__not_in' => array($post->ID), 
'showposts'=>5 // 
); 
$my_query = new wp_query($args); 
if($my_query->have_posts()) { 
echo '<h3>Current category</h3><ul>'; 
while ($my_query->have_posts()) { 
$my_query->the_post(); 
?> 
<a href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ? >"><?php the_title(); ?> 
<img src="<?php echo first_image() ?>"title="<?php the_title(); ?>" alt="<?php the_title(); ?>"/> 
</a> 
<?php 
} 
echo '</ul>'; 
} 
} 
?> 
+1

시도한 현재 코드를 알려주십시오. –

+0

전체 게시물 내용을 원하면 the_content()를 추가해야합니다. – enapupe

답변

0

이 그냥 단순히 현재의 범주를 얻을 :) 시도하고 ... 거의 좋은,하지만

해결책을 찾을 게시물의 제목을 얻을 -

<?php 
$infocat = get_the_category(); 
$info = $infocat[0]->cat_ID; 
$array = "orderby=rand&showposts=10&cat=$info"; 
query_posts($array);  
if (have_posts()) : while (have_posts()) : the_post(); ?> 
<a class="permlinccat" href="<?php the_permalink() ?>" title="Перейти к посту: <?php the_title(); ?>" ><?php the_title(); ?></a> 
<?php endwhile; else: ?> 
<?php endif; wp_reset_query(); ?> 

이 기능을 시도 현재 게시물을 쿼리에서 제외하십시오.

<?php 
    $cat = get_the_category(); 
    $catOK = $cat[0]->cat_ID; //if multiple categories 
    $postID = $post->ID; 
    query_posts(
     array(
      'cat' => $catOK, 
      'post__not_in' => array($postID) 
      ) 
     ); 
    while (have_posts()) : the_post(); ?> 

    YOUR HTML CODE 

    <?php endwhile; ?> 
+0

구문 분석 오류 : 예기치 않은 구문 오류 T_ENDIF – Pipa

+0

오 죄송합니다. 추가하는 습관이 있습니다. 및 endif; 'endif;를 삭제하십시오. – AndrePliz

+0

이 작동합니까? – AndrePliz

관련 문제