Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Display Only Child Category in your WordPress Post Loop

Last updated on by
Elegant Themes
How to Display Only Child Category in your WordPress Post Loop

In our previous post, we shared how you can only display parent category in your WordPress Post Loop. This article is the continuation of the similar technique except here we will try to only show child category in your WordPress loop. Unfortunately the_category() does not have any optional parameters like child=0 or depth=-1. When creating our WordPress Gallery, this was one of the issues we had to deal with to organize our single site pages the way we wanted.

To display only Child Category in the post loop (mostly single.php), all you have to do is replace the following code:

<?php the_category(', '); ?>

with this code:

<?php
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(10, $childcat)) {
echo '<a href="'.get_category_link($childcat->cat_ID).'">';
 echo $childcat->cat_name . '</a>';
}}
?>

Remember to change the number 10 to your parent category’s ID.

In our gallery’s case, we had the parent category called Theme Framework, and bunch of child categories. Each post was only going to be assigned one child category for the framework (for example Genesis). So this code worked out perfectly. See the live example by clicking the image below:

Display Only Parent Category in Your WordPress Loop

Hope this trick solves your problem as well.

Reference:

WordPress Codex


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. 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!

  2. GoranJakovljevic says:

    is it possible to do this for 2 categories instead of single one?

  3. gashface says:

    How Would I include &orderby=ID ?

  4. 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.

  5. 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.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.