最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to exclude children categories of parent category

programmeradmin0浏览0评论

I want to list only certain categories and exclude a particular parent and all its children. The reason is so that if a new child category is added, it won't show up by default. With wp_list_categories you also get an active class but with get_categories you don't. I would also like to somehow get an active class from it if possible.

I tried this:

$categories = get_categories(array(
    'exclude' => array(40)
));

But it still shows the children of category with ID 40.

The below code hides all the children but is a pain to try and style it whereas if I run my own loop with get_categories I can style the output much easier.

<?php wp_list_categories( array(
    'orderby'    => 'name',
    'show_count' => false,
    'exclude'    => array( 40 )
) ); ?> 

I want to list only certain categories and exclude a particular parent and all its children. The reason is so that if a new child category is added, it won't show up by default. With wp_list_categories you also get an active class but with get_categories you don't. I would also like to somehow get an active class from it if possible.

I tried this:

$categories = get_categories(array(
    'exclude' => array(40)
));

But it still shows the children of category with ID 40.

The below code hides all the children but is a pain to try and style it whereas if I run my own loop with get_categories I can style the output much easier.

<?php wp_list_categories( array(
    'orderby'    => 'name',
    'show_count' => false,
    'exclude'    => array( 40 )
) ); ?> 
Share Improve this question edited May 13, 2019 at 7:33 user10980228 asked May 13, 2019 at 7:14 user10980228user10980228 1691 silver badge14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
$categories = get_categories( array(
    'exclude_tree' => array(40)
));

In both get_categories() and wp_list_categories() you can use the exclude_tree parameter instead of exclude.

  • exclude_tree
    (array|string) Array or comma/space-separated string of term ids to exclude along with all of their descendant terms. If $include is non-empty, $exclude_tree is ignored.
    Default empty array.
发布评论

评论列表(0)

  1. 暂无评论