让wordpress首页不显示某一分类文章
有些时候为了特定内容的需要,我们希望某个分类的内容不会在 WORDPRESS 首页显示更新出来的内容,而是在第二页开始展示。WordPress 开发时也经常使用这样的功能,所以把这个方法整理分享到博客中,以便以后还需要使用直接复制。
方法一:
- //要解决这一问题,我们要用首页模板里面的 query_posts 函数:后台 – 外观 – 编辑 – 首页模板(index.php)
- <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
- //修改为:
- <?php if ( have_posts() ) : query_posts($query_string .'&cat=-20,-22'); while ( have_posts() ) : the_post(); ?>
- //直接在当前主题模板的首页 index.php 中修改调出代码,比如上面的代码中是让 20 和 22 分类不显示出来。
方法二:
- <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
- //如果不想在首页显示某一个分类的文章,只需要在这句代码下面再加一句
- <?php if (is_home() && in_category(‘1’) ) continue; ?>
- //把其中的 in_category(’1′) 这里的数字改成你不想显示出来的分类的 ID 就可以了。
方法三:
- //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 添加上面的脚本,修改对应的分类排除。
也想出现在这里?联系我们吧
