I'm trying to have the parent_category class in my loop. I've tried this:

<a href="#" class="<?php $parent = get_category($cat); echo $parent->category_nicename;?>">
    <?php single_cat_title('') ?>

and many other instead of $parent but can not make this work. How can I show the parent category class?

1 Answer 1


First you would need to check if the category has children, if it does then we can show a few things. Below is a helpful function to check just that.

function category_has_children( $term_id = 0, $post_type = 'post', $taxonomy = 'category' ) {
    $children = get_categories( array( 'child_of' => $term_id, 'type' => $post_type, 'taxonomy' => $taxonomy ) );
    return ( $children );

By adding this somewhere in your functions.php file, you can then call it and pass it a few things. It returns either true the category does have children or false if not. Below I'm assuming the $cat that you're passing into get_category() in your question above is a parent category ID, if not you need to somehow get the parent ID ( if $cat is an object, check if $cat->parent is something other than 0 ). If $cat has a parent we know it has children, if the parent is 0, we must check if it has children.

    $cat           = get_queried_object();
    $classes        = "current_{$cat->slug}";
    if( 0 == $cat->parent ) {
        $classes   .= ( category_has_children( $cat->term_id ) ? ' has_children' : '' );
    } else {
        $parent     = get_category( $cat->parent );
        $classes    = "parent_{$parent->slug} is_child";
        $classes   .= ( category_has_children( $cat->term_id ) ? ' has_children' : '' );
    <a href="#" class="<?php echo $classes; ?>"><?php echo single_cat_title( '' ); ?></a>

First we apply a class of the current category. IF the current category does not have a parent ( is a top-level category ) then we check if it has children and apply a class if it does. ELSE the current category does have a parent so we first get the parent and add 2 classes, 1 of the parent slug and another letting us know that it is a child. Finally, IF the child category also has children, add the class has_children ELSE do nothing.

get_queried_object() Codex - This will explain it in more depth but basically ( pulled from The Codex ) will get some generic information:

  • if you're on a single post, it will return the post object
  • if you're on a page, it will return the page object
  • if you're on a category archive, it will return the category object
  • if you're on an author archive, it will return the author object
  • Hi Thank you, I' mtrying to make it work ... It's fine for the parent_category. However, when it's a chidl category, it give the class 'no_children' i woudl like to give the parent_category slug when it's a child category . .. do you know how to make this work ? THank you got your help
    – tibewww
    Commented Feb 23, 2015 at 9:15
  • @user3439889 Try my edit, the $classes uses a short-hand if-else so you would want to add your child classes on the : ( else ) statement.
    – Howdy_McGee
    Commented Feb 23, 2015 at 14:25
  • hm whith your edit ti display the class of the current category_slug .. not the parent slug :( Do you ahve any other help :( ? What i want is to display only the current parent_slug :(
    – tibewww
    Commented Feb 23, 2015 at 14:32
  • @user3439889 What's the exact classes it's showing? Both the true and the false if the if-else show the parent slug, if $cat is indeed the parent as it appears in your original question.
    – Howdy_McGee
    Commented Feb 23, 2015 at 14:36
  • 1
    thank you :) it s good to know :) really appreciate your time !
    – tibewww
    Commented Feb 23, 2015 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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