Developer News, More

Displaying posts from third level sub categories in WordPress


Content segregation is a vital aspect of every website and blog. Thanks to the high-performance.content management system i.e. WordPress it has become convenient to categorize your posts in accordance to a specific topic/idea. The only downside associated with WordPress is that it currently doesn’t provide you any easy method for displaying subcategories.

If you’re a beginner in WordPress, then this problem will undoubtedly rack your brains. Well, this is a post focused on understanding the basic issue associated with the third level sub-categories in WordPress and how you can display posts from these third level sub-categories.

Displaying posts from third level sub categories in WordPress


Digging deep into the problem associated with third level sub-categories in WordPress

Let us understand this issue using a simple example. Say, you have a WordPress website which comprises of a category structure that includes a Parent Category which further includes a Sub Category. Next, this Sub Category includes third level sub-categories namely: Sub Child Category 1, Sub Child Category 2,….and so on. Have a look at the structure:

  • Parent Category
    • Sub Category
      • Sub Child Category 1
      • Sub Child Category 2

Now, the issue here is that while accessing a particular category page, you’ll be able to view the sub-categories for the chosen category. But, all the sub-categories(like sub child category 1, sub child category 2,,,,, in this post) available within a sub-categories would not be visible. A large majority of WordPress website owners have often complaint about not being able to view the sub child categories within a particular sub-category.

Take a look at the below screen-shot to grab a better understanding of third level sub-categories available in WordPress:


If you’re still confused what the third level sub-categories mean, here’s another piece of explanation. For instance, let us assume that the website navigation menu include a parent category called “Cars”. Then, there is another sub-category of this parent category called “Hatchbacks”(child category) which contains two more categories called “Hyundai” and “Ford Figo”. Now, when users would be accessing your website’s homepage, they’ll just be able to view the “Cars” category along with its sub-category called “Hatchbacks”. On clicking “Hatchbacks”, nothing would be displayed on the screen. So, this is the problem which can be resolved by including a simple piece of code into your WordPress theme’s single.php file.

Here is the code snippet that allows you to display posts from the third level sub-categories available in the sub-category of the chosen post category:


$category = get_the_category( $post->ID );

$category_name =  $category[0]->category_name;

$top_category = $category_name;

$category = get_term_by(‘name’,$top_category,’category’);

if( $category ) :

$sub_cats = get_categories(‘parent=’.$category->term_id);

if( $sub_cats ) :

foreach( $sub_cats as $sub_cat ) {

$sub_child_cats = get_categories(‘parent=’.$sub_cat->term_id);

if( $sub_child_cats ) :

foreach( $sub_child_cats as $sub_child_cat ) {

$args = array(

‘category__in’ => array($sub_child_cat->term_id),

‘posts_per_page’ => -1


$sub = new WP_Query( $args );

if( $sub->have_posts() ) { ?>

<div class=”row”>

<h2> Related Post</h2>


<?php while( $sub->have_posts() ) : $sub-> the_post(); ?>


<?php the_title(); ?>


<?php endwhile; ?>



<?php }









On adding the above code snippet to your single.php file, you’ll be able to list the child categories of all the sub-categories that have been included within different sub-categories that are located on a web page.

As is visible within the above code, “$category = get_the_category( $post->ID )” will fetch the category of current post. Then, there is “$category_name = $category[0]->cat_name;” which will fetch the sub-category of the post that is being currently. Lastly, there is “$top_category = $category_name;” which will fetch the child of the chosen sub-category i.e. Related Posts in this tutorial.

Within your single.php template file, you can go ahead with pasting the above code snippet to places where you want to display the categories. Here’s a screen-shot for how the posts would be displayed upon successful execution of the above code snippet:


That’s it!

See More: How To Improve Your WordPress Site SEO


Whether you’re planning to run a WordPress website or are already operating one, having a three level category structure would no more appear daunting with the information served in the above post. I seriously hope this tutorial would allow you to get on with adding categories and sub-categories to your website or blog in a fuss-free way.

See More: Best WordPress Popup Plugins and Why You Should Try Them

Author Biography:

Samuel Dawson has so much experience in the development of various web applications. He is currently a Senior developer in front end technologies. Samuel is an old professional in Designs2HTML Ltd- a PSD to WordPress conversion service company with great results. Samuel loves to make various strategies on developing projects.

You Might Also Like