2012-01-04 87 views
0

我的主页上有一个帖子列表,按时间顺序显示所有帖子(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> 

回答

0

解决方案1 ​​

以下内容添加到您的args阵列:

$args = array('category' => '-id', ...); 

哪里id是要排除的类别的类别ID。该解决方案不会减少所请求的帖子数量。通过$category

<?php 
    $category = get_the_category(); 
    if ($category[0] -> cat_name == 'exclude_category_name') continue; 
?> 

注意,如果帖子有多个类别,你要循环:

解决方案2

添加以下的foreach环内的开始数组并检查每个元素。

+0

这似乎是从最初被调用的5个帖子中排除来自'Events'的帖子。我希望排除的帖子在列表中替换为符合条件的类别中较旧的帖子。 – 2012-01-04 02:36:30

+0

由于您的'$ postlist'是由'get_posts()'函数调用构建的,因此您无法使用此处的代码将新条目添加到'$ postlist'中。你必须改变'get_posts()'的工作方式 – Julien 2012-01-04 03:00:51

+0

欢呼声,我设法使用下面的代码使查询符合我的要求[link](http://justpaste.it/o0y) – 2012-01-04 03:09:57