WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was hoping to do all of this within the category.php template, with help from the functions.php if needed.

If my setup is:

Parent 1
--Child 1
--Child 2
Parent 2
--Child 3
--Child 4
Parent 3
--Child 5
--Child 6

...and so on I would like to be able to do this:

If I was viewing Parent 1 (or 2/3), it would show me Child 1 and Child 2. However, I don't want to use wp_list_categories because for each Child, I would like to show a thumbnail (from a plugin function), the Child's name, and the category description.

If I was viewing Child 1 (or 2/3/4/5/6), it would show me the latest 2 posts using the standard Loop.

I would like avoiding having to do this using hard-coded contitionals with the specific names of the categories, in case new ones will be added in the future.

Any assistance will be appreciated. Thank you!

share|improve this question
up vote 0 down vote accepted

Solved, thanks to nofearinc on the wp.org irc chat! :)



$cat_id = get_query_var('cat');

if( ! empty( $cat_id ) ) {
    $category = get_category( $cat_id, ARRAY_A );

    if( ! empty( $category ) ) {
        // parent category
        if( $category['parent'] === '0' ) {

            // get child IDs
            $terms = get_term_children( $cat_id, 'category' );

            foreach( $terms as $term_id ) {
                $child_category = get_term( $term_id, 'category' );

                // if you need the category
                $category_name = $child_category->name;

                 * Sample data in your child category object: 
                 * object(stdClass)[365]
                          public 'term_id' => string '14' (length=2)
                          public 'name' => string 'Child 1' (length=7)
                          public 'slug' => string 'child-1' (length=7)
                          public 'term_group' => string '0' (length=1)
                          public 'term_taxonomy_id' => string '14' (length=2)
                          public 'taxonomy' => string 'category' (length=8)
                          public 'description' => string '' (length=0)
                          public 'parent' => string '12' (length=2)
                          public 'count' => string '5' (length=1)
                          public 'object_id' => string '33' (length=2)

                // do whatever you like with the categories now...

        } else { // in child category
            // do the regular loop here, if ( have_posts() ) ...
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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