我的主页上有一个帖子列表,按时间顺序(DESC)显示所有帖子。我想从这个列表中排除特定类别的帖子。我该怎么做?我的查询是…
<ul class="home-news"><?php
$args = array( 'numberposts' => 5, 'order'=> 'DESC', 'orderby' => 'post_date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<li>
<a href="<?php the_permalink() ?>">
<?php the_title(); ?>
<span>Posted on <?php the_date(); ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
解决方案1
将以下内容添加到args
阵列:
$args = array( 'category' => '-id', ...);
其中,id
是要排除的类别的类别id。这一解决办法不会减少所要求的员额数目。
解决方案2
在foreach
循环内部的开头添加以下内容:
<?php
$category = get_the_category();
if ($category[0] -> cat_name == 'exclude_category_name') continue;
?>
请注意,如果posts有多个类别,则需要循环遍历$category
数组并检查每个元素。