在帖子页面上,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
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\'
));