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.
You can get ID from slug quite easily:
$category = get_category_by_slug( 'clients' ); wp_list_categories('child_of='.$category->term_id);
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
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"); ?>
Hope that helps..