我在我的主页上有一个帖子列表,按时间顺序(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>发布于 2012-01-04 10:21:56
解决方案1
将以下内容添加到您的args阵列:
$args = array( 'category' => '-id', ...);其中id是要排除的类别的类别id。此解决方案不会减少所请求的职位数量。
解决方案2
在foreach循环内部的开头添加以下内容:
<?php
$category = get_the_category();
if ($category[0] -> cat_name == 'exclude_category_name') continue;
?>请注意,如果帖子有多个类别,则需要遍历$category数组并检查每个元素。
https://stackoverflow.com/questions/8721228
复制相似问题