我正在使用下面的脚本按标签获取相关帖子,但是注意到如果我有一张标有 white 的鸟的照片,而且还有一张标有 white 的表格,该表将显示在鸟的相关帖子部分。有没有办法只获得至少与其中一个类别匹配的帖子,所以除非他们共享一个类别,否则鸟和表不会相关?
我尝试在$ args中设置一个category__in数组,但没有运气!
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'showposts'=>6, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<?php if ( get_post_meta($post->ID, 'Image', true) ) { ?>
<a style="text-decoration: none;" href="<?php the_permalink(); ?>">
<img style="max-height: 125px; margin: 15px 10px; vertical-align: middle;" src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "Image", $single = true); ?>&w=125&zc=1" alt="" />
</a>
<?php } ?>
<?php
}
}
}
?>
答案 0 :(得分:0)
似乎Wordpress不会创建wp_term_taxonomy
和wp_term_relationships
表的多个连接,这很奇怪!
无论如何,快速入侵(效率不高)是在根据标签获取帖子后,将原始帖子类别与返回的帖子进行比较:
$tags = wp_get_post_tags($post->ID);
$cats = wp_get_post_categories($post->ID);
...
$my_query->the_post();
$resCats = wp_get_post_categories($post->ID);
$resArr = array_diff($resCats, $cats);
if(count($resArr) != count($resCats)) {
我会深入研究query_posts
方法,并且可能在有时间的时候写一个插件。