Do you want to only display the child category in your WordPress post loop?
Most WordPress themes show all the categories for a post, including parent and child categories. However, if you add lots of categories to your posts, then this can make your site look messy and stop readers from finding interesting content.
In this article, we will show you how to easily display only the child category in your WordPress post loop.
Why Display Only Child Category in Your WordPress Post Loop?
When creating a WordPress blog, you can organize your content using categories and tags.
To help readers find interesting content faster, you might even create child categories (or subcategories).
For example, if you have a travel blog, then you might create a ‘Destinations’ category and then have child categories such as ‘Europe,’ ‘America,’ and ‘Australia.’
By default, most WordPress themes show all parent and child categories for a post.
However, if you use lots of categories, then your blog pages may start to look messy and complicated. It can also make it more difficult for readers to find the category they’re interested in.
For that reason, you may want to hide a post’s generic parent categories and show only the child categories. That being said, let’s see how you can display only child categories in the WordPress post loop.
How to Display Only the Child Category for WordPress Post
To hide a post’s parent categories, you’ll need to add code to your WordPress website. If you haven’t done this before, then you can see our beginner’s guide on how to paste snippets from the web into WordPress.
First, you’ll need to find the code in your theme files that’s responsible for displaying categories. This is usually located in the single.php file inside your WordPress theme.
To help you find the template right file, please see our WordPress template hierarchy cheat sheet. When you’ve found the code that’s responsible for displaying categories, you can replace it with the following code:
// 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 only exclude parent categories, so it will continue to show any standalone categories that don’t have child categories.
When done, save your changes and then upload the file back to your web hosting server.
Now, you need to visit a post that has one or more child categories. You’ll see that the parent category is hidden, and WordPress is now showing the child categories only.
We hope this article helped you learn how to display only the child category in your WordPress posts. Next, you may want to see our guide on how to make money online blogging with WordPress or see our expert picks for the best SEO plugins and tools you should use.
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.
WPBeginner Support says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Admin
Mike says
Managed it!
foreach((get_the_category()) as $childcat) {
$parentcat = $childcat->category_parent;
if (cat_is_ancestor_of(10, $childcat)) {
echo get_cat_name($parentcat);
}
}
MIke says
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?
amnachohan says
Will it work outside the loop ?
Marian Rick says
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!
GoranJakovljevic says
is it possible to do this for 2 categories instead of single one?
gashface says
How Would I include &orderby=ID ?
Andus Beckus says
This is great thanks!
But how do you display children of all categories and not just cat 10?
Be great if someone could help with this.
Editorial Staff says
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
Mattia says
great, but if I want to show not “category 10” but “current category”?
Keith Davis says
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.