让wordpress主页不显示指定分类文章的方法

让wordpress首页不显示指定分类文章的方法

第一种办法:也是最先采用的办法,是在index.php中查找

    if (have_posts())

 

或者

while (have_posts())

 

在下面添加:

<!-- If the post is in the category we want to exclude, we simply pass to the
next post. -->
<?php if (in_category('42') && is_home()) continue; ?>

 
该代码的原理是,文章loop中遇到分类id为42的文章后立即跳过;但同时也带来一个文章列表分页问题——如果设定的是每页显示10篇文章,其中有8篇是微博分类下的“文章”,在使用上述代码后,该页文章列表仅显示2篇非微博分类的文章。假如你最近发布了10篇微博,那么你的博客首页的文章列表将会是空的!!!

下面的两种办法都是采用了query_posts函数,经过本人测试,没有上述问题,大家可以放心使用。

第二种方法:转自露兜博客,还是在index.php中查找 if (have_posts()) 或 while (have_posts()) ,在前面添加query_posts函数如下:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    // 不想显示的分类ID,多个用半角逗号隔开
    'category__not_in'   => array(42),
    'paged' => $paged
);
query_posts($args);

 

第三种办法:还是在index.php中查找 if (have_posts()) 或 while (have_posts()) ,将查找到的这一整行改成:

if ( have_posts() ) : query_posts($query_string .'&cat=-42'); while
( have_posts() ) : the_post();
//42即为不想显示的分类ID,多个用半角逗号隔开。