Displaying Posts From 3rd Level Sub Categories in WordPress


There’s no denying the fact that WordPress is a high-performance content management system that can be further elevated in its feature set, thanks to the amazing customization possibilities it provides.

No matter whether you’re using WordPress for creating any website or a blog, you may would like to display subcategories in your category pages. Segregating content is an essential part of a website as well as a blog, enabling your website visitors to have a clear and concise idea about what the content is all about. But, it isn’t easy as it might appear to you!

Put it simply, WordPress does not provide any easy way to do display subcategories. In fact, finding the right solution to this problem will surely make beginners to rack their brains, as there is no defined way to meet this objective.


In this post, I will provide you with a working example to illustrate how you can display posts from 3rd level subcategories in WordPress. But, before that let’s have a more detailed view on the problem.

Understanding Problem Associated With 3rd Level Sub Categories in WordPress

In order to understand the problem associated with displaying post from sub-categories in a WordPress site, let’s say, for example, you’ve a WordPress website with the category structure that looks something like:

  • Parent Category 1
    • Sub Child Category 1
      • Sub-Sub Child Category 1
      • Sub-sub Child Category 2

Now, when you access the category page you will be able to see the subcategories, however, you might not be able to view any category inside that subcategory (i.e. Sub-Sub Child Category 1 and later). As a matter of fact, a lot of website owners often complain encountering this issue in their WordPress powered sites.

Still can’t understand the terminology?

Let me explain this way, let’s assume you website navigation menu contains a (parent) category called “Design”. You have one subcategory (child of category) under Design called “Web Design” that contains two categories (child of sub-category): “Animation” and “Graphic Design”. When users will access your homepage, they will only see Design category and its subcategory: Web Design. But, when they’ll click on Web Design nothing will be visible.

And so, to resolve this issue I’ll be writing a code that will allow you to list child of subcategories inside the subcategory.

How to Display Posts From 3rd Level Sub Categories?

You will have to paste the following code in your theme’s single.php file:

	$cat = get_the_category( $post->ID );
	$cat_name =  $cat[0]->cat_name;
	$top_cat = $cat_name;
	$cat = get_term_by('name',$top_cat,'category');
	if( $cat ) :
		$sub_cats = get_categories('parent='.$cat->term_id);
		if( $sub_cats ) :
			foreach( $sub_cats as $sub_cat ) {
			$sub_sub_cats = get_categories('parent='.$sub_cat->term_id);
				if( $sub_sub_cats ) :
					foreach( $sub_sub_cats as $sub_sub_cat ) {
						$args = array(
							'category__in' => array($sub_sub_cat->term_id),
							'posts_per_page' => -1
						$sub = new WP_Query( $args );
						if( $sub->have_posts() ) {
							echo '<div class="entry-content">';
							echo '<h2>Related Post</h2>';
							echo '<ul>';
								while( $sub->have_posts() ) : $sub->the_post();
									echo '<li>';
									echo '</li>';
							echo '</ul>';
							echo '</div>';

As you can see in the code, “$cat = get_the_category( $post->ID )” will get the category of the current post. And then, “$cat_name = $cat[0]->cat_name;” will get the sub-category of the post. Finally, “$top_cat = $cat_name;” will help to get the child of the sub-category i.e. Related Posts in our example. You can paste the above code wherever you would like to display your categories, but make sure to add the code in your single.php template file.

Once the get code is executed, the 3rd level subcategory will be visible to you (as shown in the below screenshot).

This can prove to be an immensely useful hack, when you’re planning to create a WordPress powered website.


Wrapping Up!

Are you running a WordPress website or planning to create one? Do you have a category structure that is 3 level down? Well, then when returning the categories for the post you may encounter a problem: you might not be able to view your sub-categories. Hopefully, this tutorial will help you with a possible solution to overcome this problem.

Categories and sub-categories are an essential part of a website or a blog, and so you should know how to add them in the right manner.

Displaying Posts From 3rd Level Sub Categories in WordPress
5 votes, 2.60 avg. rating (56% score)
  • Posted in: Tutorials, WordPress

Posted by

Sophia is a renowned WordPress developer by profession and is currently handling a suite of WordPress development projects. In case, you have made up your mind to enhance WP website then it is better to Hire WordPress Developer who can serve you the desired services. Sophia already has multiple WordPress-related articles under her name.