[英]WordPress: Related Posts by Tags, but in the Same Categories
我使用下面的脚本来获取相关的标签职位,但注意到,如果我有一只鸟的图片标记白表也标记的白色 ,该表将在鸟的相关职位部分显示出来。 有没有办法只获得至少与其中一个类别相匹配的帖子,因此鸟类和表格不会相关,除非它们共享一个类别?
我尝试在$ 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
}
}
}
?>
似乎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
方法,并在有时间的时候编写插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.