2010-06-08 102 views
1

我試圖根據客戶分類標準顯示相關帖子。我在wordpress.org發現了一個查詢,這種作品。但是,原始帖子會多次在結果中重複。 (單詞是我使用的自定義分類的名稱)似乎發生的情況是單個帖子根據showpost設置的數量得到重複。任何想法都可能導致這種情況?自定義分類問題的WordPress相關帖子

代碼:

<?php 
//for in the loop, display all "content", regardless of post_type, 
//that have the same custom taxonomy (e.g. words) terms as the current post 
$backup = $post; // backup the current object 
$found_none = '<h2>No related posts found!</h2>'; 
$taxonomy = 'words';// e.g. post_tag, category, custom taxonomy 
$param_type = 'words'; // e.g. tag__in, category__in, but genre__in will NOT work 
$post_types = get_post_types(array('public' => true), 'names'); 
$tax_args=array('orderby' => 'none'); 
$tags = wp_get_post_terms($post->ID , $taxonomy, $tax_args); 
if ($tags) { 
    foreach ($tags as $tag) { 
    $args=array(
     "$param_type" => $tag->slug, 
     'post__not_in' => array($post->ID), 
     'post_type' => $post_types, 
     'showposts'=>5, 
     'caller_get_posts'=>1 
    ); 
    $my_query = null; 
    $my_query = new WP_Query($args); 
    if($my_query->have_posts()) { 
     while ($my_query->have_posts()) : $my_query->the_post(); ?> 
     <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3> 
     <?php $found_none = ''; 
     endwhile; 
    } 
    } 
} 
if ($found_none) { 
echo $found_none; 
} 
$post = $backup; // copy it back 
wp_reset_query(); // to use the original query again 
?> 

回答

0

這是foreach循環,你要重複內。該代碼有效地說,

  1. 獲取分類類型的所有條款$param_type
  2. 對於每個項,獲得5個職位,都標記有長期

所以,如果你有一個標記有超過一個職位一個相同的分類術語,它可能會出現不止一次。

您可以反覆將查詢的帖子添加到post__not_in數組中以確保它們不會再次出現;

  1. 添加$post_not_in = array($post->ID);略高於if ($tags) {

  2. 然後用post__not_in' => $post_not_in,更換線post__not_in' => array($post->ID),

  3. 最後,下降$post_not_in[] = get_the_ID();while循環內,後$found_none = '';

+0

嗨DeadMedic, 感謝您的回覆。我做了調整,但不幸的是我仍然得到重複。它似乎完全忽略了代碼$ post_not_in = array($ post-> ID);如果我把它放進去也沒關係,結果是一樣的。 這可能是一些簡單的錯誤,但我不知道它是什麼。 – Nordin 2010-06-08 15:15:14

+0

你正在使用哪個版本的WordPress? – TheDeadMedic 2010-06-08 15:35:46

+0

我正在使用2.9.2 – Nordin 2010-06-08 17:54:19

0

至於我,我使用this plugin爲自定義分類相關崗位。我希望這個插件能夠幫助你解決問題。

相關問題