Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Display Only Child Category in Your WordPress Post Loop

Do you want to only display the child category in your WordPress post loop?

Most WordPress themes will automatically show all categories for a post including parent and child categories. What if you only wanted to display the child category?

In this article, we’ll show you how to easily display only the child category in your WordPress post loop.

Showing only child categories inside WordPress post loop

Why and When You Would Want to Show Only The Child Category

WordPress comes with two default taxonomies called categories and tags to organize your content. Many websites use tags for specific topics of each article and categories for broader website sections.

Then there are websites that use categories to define the structure of their website. For instance, a travel website may use categories for different types of destinations, or a food blog may use them for different types of cuisines.

Categories are hierarchical too, which means you can create child categories (or subcategories) for a parent category to further organize your content. For instance, a travel website may file an article under Destinations » Europe where Europe is the child category.

By default, a WordPress theme would display all parent and child categories for a post.

All child and parent categories displayed

However, listing all categories for a post may not look as neat and focused as displaying the most relevant category. In that case, you may want to skip the parent category and only display the child category instead.

That being said, let’s take a look at how to only display the child category for a WordPress post.

Only Displaying The Child Category for a WordPress Post

This tutorial requires some basic knowledge of copy and pasting custom code snippets in WordPress.

First, you’ll need to find the code in your theme files responsible for displaying categories. This is usually located in the single.php file inside your theme.

To learn more, see our WordPress template hierarchy cheat sheet which helps you find out which template files are used to display different sections in a WordPress theme.

Once you have located the code responsible for displaying categories, you can replace it with the following code.

// Get the IDs of child categories if any
$categories = get_the_category();
foreach( $categories as $category ) {
If ( $category->parent > 0 ) { 
$child_cat_ID[] =  $category->term_id; 
}
} 

// If there are no child categories then display categories
If ( empty($child_cat_ID)) { 
echo get_the_category_list( ' , ', '' );

// display child categories only
} else { 
	
$child_cat_IDs = implode(', ', $child_cat_ID);
echo '<div class="post-categories">Filed under: ';
wp_list_categories( array(
        'separator' => ' ',
		'style'		=> '',	
        'include' =>  $child_cat_IDs  
    ) );
echo '</div>';
} 

Don’t forget to save your changes and upload the theme files back to your server.

You can now visit your single post which has one or more child categories. You’ll notice that it will hide the parent category and only display child categories.

Only child categories displayed

There is one problem with this code.

If you have selected a parent category with child categories, and another single category, then the code will skip the standalone category. That means that in the example below, the “News” category wouldn’t be displayed.

Standalone category

If this is an issue for your design needs, then you can use the following code instead.

// Get the IDs of categories
$categories = get_the_category();
foreach( $categories as $category ) {
If ( $category->parent > 0 ) { 
$child_cat_ID[] =  $category->term_id; 
//store child's parent category id to use later
$parent_cat_ID = $category->parent;
} 
// find categories with no parent and no child
If ( $category->parent == 0 && $category->term_id !== $parent_cat_ID) { 
$single_category[] = $category->term_id; 
}
}
				
// Display part of the code
			
// if there are no child categories then go ahead and display all categories. 
If ( empty($child_cat_ID)) { 
echo get_the_category_list( ' , ', '' );
} 

// If there are child categories then do this				
else { 
$child_cat_IDs = implode(', ', $child_cat_ID) ;
$single_category_id = implode(', ', $single_category);	

// Combine child categories and categories with no children
$cats_to_include =   $child_cat_IDs . ', ' . $single_category_id ; 

// Display them 
echo '<div class="post-categories">Filed under: ';

wp_list_categories( array(
'separator' => ' ',
'style'	=> '',	
'include' =>  $cats_to_include 
 ) );
echo '</div>';

} 

This code will now only exclude parent categories. It will continue to show any standalone categories along with child categories.

Child categories and single standalone categories displayed

We hope this article helped you learn how to display only child category in your WordPress posts. You may also want to try your hand on these useful tricks for WordPress functions file or consider using these powerful WordPress page builders to avoid writing any code at all.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

10 CommentsLeave a Reply

  1. Managed it!

    foreach((get_the_category()) as $childcat) {
    $parentcat = $childcat->category_parent;
    if (cat_is_ancestor_of(10, $childcat)) {
    echo get_cat_name($parentcat);
    }
    }

  2. I have three main categories and this code is successfully working in my single page loop to echo the actual selected category name.
    I now want to echo the parent of the category. The complication is that I have two layers below the main category (3 levels) and I want to echo the one level parent not the top level parent. It seems easy to echo the top parent, but I haven’t seem any code to return the child level category of a grandchild category?

  3. This is a great piece of code. Thanks a lot so far!

    For one of my projects I have to go further, and display only the lowest subcategory. So there may be three levels, (Forms -> Squares -> Big Squares). With this code all subs (Squares -> Big Squares) are displayed. How can I tell this code to repeat the process till only the last child is found and displayed?

    If you’ve got any solutions for that you are my heroes once again! Keep up your great work and blog!

    • If you are trying to display a list of all child categories, then use wp_list_categories() function. It has parameters that allow you to list only child categories or only parent categories. But that doesn’t work for the case that we are talking about in this article.

      Admin

  4. Great snippets of info from you guys.
    I really have to start to get into this PHP.

    Great site boys and I notice that you are up to Pagerank 6!
    How about a couple of posts on upping your pagerank.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.