Get Child Categories of Parent Category on Single Post in WordPress

I was hoping to find a function in the WordPress API, that goes something like this…

  the_child_categories("Vermont");
  // print Burlington, Brattleboro... etc if this post has any child categories of Vermont

But I could not. The result that I did find, from various forums goes something like this…

$parentcat = get_category_by_slug('Vermont');
foreach((get_the_category()) as $childcat):
  if (cat_is_ancestor_of($parentcat, $childcat)):
    echo get_category_link($childcat->cat_ID);
    echo $childcat->cat_name;
  endif;
endforeach;

If you’re in the in the Loop, you can use the get_the_category() function to retrieve the category objects of the current post, without printing the results to the screen. You can loop over the contents from what is returned by the function.

This condition uses the API function “cat_is_ancestor_of” to check whether or not the category from the current post is a child of the parent which we fetched with the “get_category_by_slug” function.

Here is a little function that I threw into “functions.php” so that I can get the child categories easily in my theme.

/** Usage
_the_category_children("Vermont"); */
function _the_category_children($slug=""){
  if($categories       = get_the_category()):
    if($slug_category   = get_category_by_slug($slug)):
      foreach($categories as $category):
        echo (cat_is_ancestor_of($slug_category, $category)) ? $category->cat_name : '';
      endforeach;
    endif;
  endif;
}

More info/references on accomplishing this functionality are available
http://codex.wordpress.org/Function_Reference/cat_is_ancestor_of
http://wordpress.org/support/topic/284057?replies=8#post-1120489
http://wordpress.org/support/topic/297615

  • http://seanbehan.com Sean Behan

    This might me a little better, since it uses the WP API.

    $mom = get_category_by_slug("Vermont");
    $son = wp_category_list('child_of='.$mom->cat_ID);
    
  • admin

    …my mistake, this will not show the child categories of the parent for the single post… it will only retrieve the children of the parent category regardless of which category the post is in…

  • http://www.origamifolder.com David

    With taxonomies, terms can be used to get categories:
    get_term_children( $term, $taxonomy ) ;

    Ex. get_term_children(‘cat_ID’, ‘category’ ) ;
    Grabs children of the category of the ID you show
    http://codex.wordpress.org/Function_Reference/get_term_children

  • http://www.origamifolder.com David

    Oh, and if you want the cats children in a list:
    wp_list_categories(‘child_of=0′);

    http://codex.wordpress.org/Template_Tags/wp_list_categories

  • http://tutoorials.com voo

    $categories = get_categories( $args );

    $args = array(
    ‘type’ => ‘post’,
    ‘child_of’ => 0,
    ‘parent’ => ,
    ‘orderby’ => ‘name’,
    ‘order’ => ‘ASC’,
    ‘hide_empty’ => 1,
    ‘hierarchical’ => 1,
    ‘exclude’ => ,
    ‘include’ => ,
    ‘number’ => ,
    ‘taxonomy’ => ‘category’
    ‘pad_counts’ => false

    http://codex.wordpress.org/Function_Reference/get_categories

  • niksos

    great the code works for me. I’m trying to query multiple categories and children for each separately on single.php I’d need to add link though. Any ideas?