WordPress实现“首页和page页不显示某个分类下(栏目下)的文章”方法

作者: admin 分类: wordpress主题制作教程 发布时间: 2022-02-18 23:41

不用插件,只要在模板代码中加入相关代码就可以实现“首页不显示某个分类下(栏目下)的文章”,方法如下:

方法1、在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('12') && is_home()) continue; ?>

文章loop中遇到分类id为12的文章后立即跳过;如果你设置了首页显示10篇文章,
但是如果你其他分类下没有文章,首页会显示空白,切记住。

下面的两种办法都是采用了query_posts函数。

方法2:转自露兜博客,还是在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(12),
        'paged' => $paged
    );
    query_posts($args);

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

  if ( have_posts() ) : query_posts($query_string .'&cat=-12'); while
    ( have_posts() ) : the_post();

12即为不想显示的分类ID,多个分类请用半角逗号隔开。

方法一,应该是不在首页显示某个栏目的文章,但是page页面还是会显示的;方法二和方法三,应该是在首页和page页都不会显示某个分类栏目下文章的。目前还没有测试,但是肯定能用的到,先记录下。

补充:我采用的是这个方法(方法三),代码如下:

<?php if( have_posts() ) : query_posts($query_string .'&cat=array(-73,-33,-65,-45,-54)');while( have_posts() ) : the_post(); ?>

如果不想某个栏目的文章显示在首页或者page页,那就找到这个栏目的ID,在ID前面加一个负号,填到上面的代码中就可以了。上面我这里设置了5个栏目不显示。

相关文章

发表评论

邮箱地址不会被公开。 必填项已用*标注

Powered by 草根站长笔记 © 2015-2020 草根站长笔记 Inc.版权所有,禁止转载