仅获取当前类别中的一个

时间:2012-12-15 作者:Ben

在帖子页面上,Im使用以下代码显示指向当前类别中其他帖子的链接。

我的问题是,当当前帖子位于多个类别中时,它将仅显示第一个类别中的帖子。

我想通过排除某些类别ID(例如29、35)来控制显示帖子的当前类别

任何想法都将不胜感激。

<?php
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array(\'numberposts\' => 5, \'offset\' => 0, \'category__in\' => array($category),  \'post_status\'=>\'publish\', \'order\'=>\'ASC\' ));
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php echo get_post_meta($post->ID, "rw_post_type", true); ?></a>
</li>
<?php endforeach; ?>
<?php wp_reset_query(); 

?>
</ul>
<?php

1 个回复
SO网友:s_ha_dum

get_the_category 应该为您的帖子返回所有类别,但您只选择其中一个,第一个,当您这样做时--$category = $category[0]->cat_ID;. 请尝试以下操作,而不要使用该行:

$allcats = array();
foreach ($category as $cat) {
  if (29 != $cat->cat_ID && 35 != $cat->cat_ID) {
    $allcats[] = $cat->cat_ID;
  }
}
$myposts = get_posts(
  array(
    \'numberposts\' => 5, 
    \'offset\' => 0, 
    \'category__in\' => $allcats,  
    \'post_status\'=>\'publish\', \'order\'=>\'ASC\' 
));

结束

相关推荐

Match two posts in categories

我有博客帖子,每一篇都有三个或三个以上的类别。问题是我需要搜索相关帖子,我必须匹配至少两个类别,即必须有两个常见类别。我正在使用此查询: $args = wp_parse_args($args, array( \'showposts\' => 10, \'post__not_in\' => array($post_id), \'ignore_sticky_posts\' => 1, \'category__in\' =&