2012-03-13 67 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