让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,多个用半角逗号隔开。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值