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 )
) ); ?> 

1 Answer 1

$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.
  • Thank you, I also managed to get it working using 'parent' => 0 May 13, 2019 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.