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
1 Answer
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.