2014-04-01 36 views
0

例如,我有一些類別包含5個帖子。當我選擇帖子時,需要顯示其所在類別的其餘四個帖子。如何在single.php上顯示此single.php所在類別的所有帖子

<?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 
$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 
    $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

相關問題