2012-03-13 64 views
0

有沒有辦法從相關的帖子循環中排除某些標籤,而它仍然可以找到其他標籤?這裏是我有的代碼,但我知道沒有像tag_slug__not_in這樣的值,但我不想使用ID的,因爲它們很混亂,是否沒有辦法通過slug排除標籤,並且通常這會工作,因爲我我說包括的所有標籤都是?任何幫助非常感謝!相關帖子WordPress的循環排除某些標籤

<?php //for use in the loop, list 5 post titles related to first tag on current post 
$backup = $post; // backup the current object 
$tags = wp_get_post_tags($post->ID); 
$tagIDs = array(); 
if ($tags) { 
$tagcount = count($tags); 
for ($i = 0; $i < $tagcount; $i++) { 
    $tagIDs[$i] = $tags[$i]->term_id; 
} 
$args=array(
    'tag__in' => $tagIDs, 
    'tag_slug__not_in' => array('investing', 'investment', 'travel', 'shopping', 'retail',  'organisations', 'governments', 'government', 'individuals', 'entrepeneurs', 'companies', 'markets', 'finance', 'clean-tech', 'money', 'world', 'business'), 
    'post__not_in' => array($post->ID), 
    'showposts'=>5, 
    'caller_get_posts'=>1, 
    'post_type' => array('post','indepth','feature','interview') 
); 
$my_query = new WP_Query($args); 
if($my_query->have_posts()) { ?> 
<h3>Related Articles</h3> 
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 
    <ul><li><p><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></p></li> 
    </ul> 
    <?php endwhile; 
} else { ?> 

<?php } 
} 
$post = $backup; // copy it back 
wp_reset_query(); // to use the original query again 
?> 

回答

1

我同意,他們沒有tag_slug__not_in參數是非常愚蠢的。

無論如何,我認爲你可以使用WP_Query的tax_query參數來實現你想要做的事情。下面的代碼是未經測試:

$args=array(
    'post__not_in' => array($post->ID), 
    'showposts'=>5, 
    'caller_get_posts'=>1, 
    'post_type' => array('post','indepth','feature','interview'), 
    'tax_query' => array(
    'relation' => 'OR', 
    array(
     'taxonomy' => 'post_tag', 
     'field' => 'id', 
     'terms' => $tagIDs 
    ), 

    array(
     'taxonomy' => 'post_tag', 
     'field' => 'slug', 
     'terms' => array('investing', 'investment', 'travel', 'shopping', 'retail', 'organisations', 'governments', 'government', 'individuals', 'entrepeneurs', 'companies', 'markets', 'finance', 'clean-tech', 'money', 'world', 'business'), 
     'operator' => 'NOT IN' 
    ) 
) 
); 

如果這不起作用,你可能想嘗試更多的閱讀了關於該Taxonomy Parameters in WP_Query,看看tax_query陣列中添加relation鍵可以在溶液,還援助你的問題。

+0

輝煌,在某種程度上起作用,但它實質上做了什麼,或多或少地將所有事物都解除關聯。我試圖做的是讓它忽略這些標籤,但要注意帖子中的其他標籤用那個標記。我不確定這是否真的有可能,儘管說實話可​​以這麼說,不要顯示帶有此標籤的帖子,當我的意思是更多時,如果他們只有這個標籤與您共用,則不顯示帖子。這有意義嗎? – Amy 2012-03-14 00:03:46

+0

我基本上希望它忽略這些標籤,但不要這些帖子,以便它仍然在這些帖子內搜索其他相關標籤。 Trickkky之一。嗯,我不確定這是可能的,現在我正在考慮它。 – Amy 2012-03-14 00:12:08

+0

檢查我更新的答案。我想這可能是你想要做的。 – chrisn 2012-03-14 00:19:16