【发布时间】:2012-03-13 23:13:55
【问题描述】:
有没有办法从相关帖子循环中排除某些标签,而它仍然可以找到其他标签?这是我拥有的代码,但我知道没有像 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
?>
【问题讨论】: