How to exclude children categories of parent category

I want to list only certain categories and exclude a particular parent and all its children. The reason is so that if a

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745499972a4630368.html

相关推荐

  • How to exclude children categories of parent category

    I want to list only certain categories and exclude a particular parent and all its children. The reason is so that if a

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信