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

时间:2018-08-23 22:53:20

标签: php wordpress

我在Wordpress网站上有2页。所有2页上均显示帖子。我需要限制首页中的几个类别,并限制第二页中的其他类别。

   <?php
    function excludeCat($query) {
      if ( $query->is_home ) {
        $query->set('cat', '-3,-5,-23');
      }
      return $query;
    }
    add_filter('pre_get_posts', 'excludeCat');
    ?>

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

2 个答案:

答案 0 :(得分:0)

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

<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

答案 1 :(得分:0)

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

<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>