2012-09-06 78 views
1

我想通過類別頁面上帖子旁邊的標籤顯示相關帖子。 所有相關的帖子我可以找到的代碼片段將用於single.php的嵌套循環,但我需要它在類別頁面上的循環。用wordpress中的分類模板頁面上的標籤顯示相關帖子

所以,當你去類別「貓」應該輸出如下: 「後1題」,類別「貓」,標籤「小貓」 「相關崗位1.1標題」標籤「小貓」 「相關發佈1.2標題」標籤‘小貓’


‘後2題’,類別‘貓’,標籤‘雄貓’ ‘相關崗位2.1標題’標籤‘雄貓’ ‘相關崗位2.2標題’ ,標籤「tomcats」


...

這是我提出的代碼,但它打破了。

'//第一個查詢 $ my_query = new WP_Query('cat = 6');

// If first query have posts 
if($my_query->have_posts()) : 

    // While first query have posts 
    while ($my_query->have_posts()) : $my_query->the_post(); 
    ?> 

    <!-- start post --> 


    <!-- End post div --> 

     <?php 
     // tags 

      $tag_ids = array(); 
          foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; 
          $args=array(
          'tag__in' => $tag_ids, 
          'post__not_in' => array($post->ID), 
          'posts_per_page'=>99, 
          'caller_get_posts'=>1 
          ); 
      // Second query 
      $my_second_query = new WP_Query('$args'); 

      // If second query have posts 
      if($my_second_query->have_posts()) : 
      ?> 

       <?php 
       // While second query have posts 
       while($my_second_query->have_posts()) : $my_second_query->the_post(); 


        ?> 
        <!-- start post --> 


    <!-- End post div --> 


        <?php 
       // End second while have posts 
       endwhile; 
       ?> 
    <?php 
    // End first while have posts 
    endwhile; 

// End if first query have posts 
endif; 
?>` 

這甚至可能嗎?我無法爲我的生活找到榜樣。 非常感謝提前

回答

1

是的,這是可能的。看起來你的代碼正處於正確的方向。您只需創建一個自定義查詢,查看當前帖子的標籤,然後使用這些查找其他帖子。只要你在循環中,它是否在single.php或其他任何地方使用都沒關係。添加到您的functions.php文件:

function echo_related_posts() { 
    global $post; 
    // Get the current post's tags 
    $tags = wp_get_post_tags($post->ID); 
    $tagIDs = array(); 
    if ($tags) { 
     // Fill an array with the current post's tag ids 
     $tagcount = count($tags); 
     for ($i = 0; $i < $tagcount; $i++) { 
      $tagIDs[$i] = $tags[$i]->term_id; 
     } 
     // Query options, the magic is with 'tag__in' 
     $args = array(
      'tag__in' => $tagIDs, 
      'post__not_in' => array($post->ID), 
      'showposts'=> 5 
     ); 
     $my_query = new WP_Query($args); 
     // If we have related posts, show them 
     if ($my_query->have_posts()) { 
      $related = ''; 
      while ($my_query->have_posts()) { 
       $my_query->the_post(); 
       $current = $my_query->current_post + 1; 
       $related .= "Related post " . $current . ": "; 
       $related .= "<a href='" . get_permalink() . "' >"; 
       $related .= get_the_title(); 
       $related .= "</a>"; 
       if (($my_query->current_post + 1) != ($my_query->post_count)) $related .= ", "; 
      } 
      echo $related; 
     } 
     else echo "No related posts"; 
    } 
    else echo "No related posts"; 
    wp_reset_query(); 
} 

很明顯,你可以在這個功能改變任何東西,讓你正在尋找確切的結果,它只是將呼應多達五個相關的職位的例子。請參閱http://codex.wordpress.org/Class_Reference/WP_Query以獲取有關自定義查詢的進一步參考。

使用該功能,您現在可以訪問echo_related_posts()函數,該函數將使用通用標記輸出任何相關的帖子。因此,在您category.php或正在使用您的類別頁面,你可以不喜歡哪個模板以下(這是一個過於簡單化的循環爲簡潔起見,剛剛記下的echo_related_posts()功能):

// Inside your existing loop 
<?php while (have_posts()) : the_post(); ?> 

    // Output the current post info here 

    // Output the related posts 
    <?php echo_related_posts(); ?> 

<?php endwhile; ?> 

假設相關的職位被發現時,輸出是這樣的:

「相關崗位1:標題1,相關崗位2:標題2,相關職位3:標題三」

希望你可以從那裏!

+0

謝謝sooo多,我剛從我的假期回來,並將繼續在這個項目上,並與結果回你! –

相關問題