首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从我的Wordpress主页中排除特定类别?

如何从我的Wordpress主页中排除特定类别?
EN

Stack Overflow用户
提问于 2018-08-24 06:53:20
回答 2查看 36关注 0票数 0

我在Wordpress网站上有两个页面。所有两个页面都显示了帖子。我需要限制几个类别在第一页和其他几个类别从第二页。

代码语言:javascript
复制
   <?php
    function excludeCat($query) {
      if ( $query->is_home ) {
        $query->set('cat', '-3,-5,-23');
      }
      return $query;
    }
    add_filter('pre_get_posts', 'excludeCat');
    ?>

这将不起作用,因为它将阻止所有2个页面的类别。

EN

回答 2

Stack Overflow用户

发布于 2018-08-24 07:00:19

我记得在最近的一个项目中做过这样的事情,这是我们的解决方案:

代码语言:javascript
复制
<div class="blog-categories">
  <h3>Categories</h3>
    <ul>
        <?php
            $args = array(
                'show_option_all'    => '',
                'orderby'            => 'name',
                'order'              => 'ASC',
                'style'              => 'list',
                'show_count'         => 0,
                'hide_empty'         => 0,
                'use_desc_for_title' => false,
                'child_of'           => 0,
                'feed'               => '',
                'feed_type'          => '',
                'feed_image'         => '',
                'exclude'            => '1',
                'exclude_tree'       => '',
                'include'            => array( 7, 8, 9, 10, 15 ),
                'hierarchical'       => 1,
                'title_li'           => 0,
                'show_option_none'   => __( '' ),
                'number'             => null,
                'echo'               => 1,
                'depth'              => 0,
                'current_category'   => 0,
                'pad_counts'         => 0,
                'taxonomy'           => 'category',
                'walker'             => null,
                'show_option_all'    => 'All',
            );
            wp_list_categories( $args );
        ?>
    </ul>
</div>

请注意特定类别ID的包含和排除。

你可以在“帖子>类别”WordPress菜单中点击某个类别,然后查看屏幕左下角的链接,找到类别ID。

Docs on wp_list_categories() here

票数 0
EN

Stack Overflow用户

发布于 2018-08-24 07:05:35

我在最近的一个项目中应用了类似的东西,这是我们的解决方案:

代码语言:javascript
复制
<div class="blog-categories">
<h3>Categories</h3>
<ul>
    <?php
        $args = array(
            'show_option_all'    => '',
            'orderby'            => 'name',
            'order'              => 'ASC',
            'style'              => 'list',
            'show_count'         => 0,
            'hide_empty'         => 0,
                // 'hide_empty'         => 1,
            'use_desc_for_title' => false,
            'child_of'           => 0,
            'feed'               => '',
            'feed_type'          => '',
            'feed_image'         => '',
            'exclude'            => '1',
            'exclude_tree'       => '',
                /*
                    ** Note: Hover over a category in 'Posts > Categories' WordPress menu and see link on bottom left of screen to find category ID
                */
            'include'            => array( 7, 8, 9, 10, 15 ),

                // 'include'            => '',
            'hierarchical'       => 1,
            'title_li'           => 0,
            'show_option_none'   => __( '' ),
            'number'             => null,
            'echo'               => 1,
            'depth'              => 0,
            'current_category'   => 0,
            'pad_counts'         => 0,
            'taxonomy'           => 'category',
            'walker'             => null,
                'show_option_all'    => 'All',
        );
        wp_list_categories( $args );
    ?>
</ul>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51995319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档