Listing Category ‘child_of’ by slug rather than ID

I want to list categories belonging to a parent. The problem is using category id’s isn’t useful and makes things harder to understand.

Is there any easier way to list categories belonging to a parent, similar to the default way supplied in the WP codec?

Read More
<?php wp_list_categories('child_of=8'); ?> //what category is '8' ?!

<?php wp_list_categories('child_of=clients'); ?> //much nicer, but doesn't work

I would prefer to avoid using another query in the loop, but to make sense to other developers, I might have to.

Related posts

Leave a Reply

2 comments

  1. Do you want all children on a set category then, as that’s essentially what the child_of parameter is for, getting all descendants of a term(category).

    If you want only terms that have a particular direct parent then you should instead be using parent.

    In regard to using an ID, this should work..

    <?php
    // Get ID for the named category
    $cat_term_id = get_cat_ID( 'my-cat' );
    
    wp_list_categories("parent=$cat_term_id");
    //wp_list_categories("child_of=$cat_term_id");
    ?>
    

    get_cat_ID docs.


    Hope that helps..