来源于:
https://guihet.com/exclude-category-home.html
有些时候为了特定内容的需要,我们希望某个分类的内容不会在 WordPress 首页显示更新出来的内容,而是在第二页开始展示。正好现在需要这样的功能,所以把这个方法整理分享到博客中,以便以后还需要使用直接复制。
第一、修改 index.php
<?php if ( have_posts() ) : query_posts($query_string .'&cat=-20,-22');while ( have_posts() ) : the_post(); ?>直接在当前主题模板的首页 index.php 中修改调出代码,比如上面的代码中是让 20 和 22 分类不显示出来。
第二、functions.php 修改
这个方法是比较好的,建议使用。
//在首页中排除某些分类function exclude_category_home( $query ) {if ( $query->is_home ) {$query->set( 'cat', '-20, -22' ); //你要排除的分类ID}return $query;}
add_filter( 'pre_get_posts', 'exclude_category_home' );这个方法直接不会有任何页面空缺问题,而且在最新内容中也不会出现。直接在当前主题的 functions.php 添加上面的脚本,修改对应的分类排除。