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?

<?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.

2 Answers
2

You can get ID from slug quite easily:

$category = get_category_by_slug( 'clients' );
wp_list_categories('child_of=".$category->term_id);

Leave a Comment