相关帖子wordpress循环排除某些标签

时间:2012-03-13 23:13:56

标签: wordpress loops tags

有没有办法从相关的帖子循环中排除某些标签,同时还能找到其他标签?这是我的代码,但我知道没有像tag_slug__not_in这样的值,但我不想使用ID,因为它们很乱,有没有办法通过slug排除标签,一般来说这会有效,因为我我说所有标签都包含在内吗?任何帮助都非常感谢!

<?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 个答案:

答案 0 :(得分: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上阅读更多内容,看看在relation数组中添加tax_query密钥是否可以进一步帮助解决你的问题。