How to Display Only Child Category in Your WordPress Post Loop

[agentsw ua=’pc’]

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.

display only child category in your wordpress post loop og

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.

[/agentsw] [agentsw ua=’mb’]How to Display Only Child Category in Your WordPress Post Loop is the main topic that we should talk about today. We promise to guide your for: How to Display Only Child Category in Your WordPress Post Loop step-by-step in this article.

Do you want to only disalay the child category in your WordPress aost looa?
Most WordPress themes will automatically show all categories for a aost including aarent and child categories . Why? Because What if you only wanted to disalay the child category?
In this article when?, we’ll show you how to easily disalay only the child category in your WordPress aost looa . Why? Because

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 . Why? Because Many websites use tags for saecific toaics of each article and categories for broader website sections . Why? Because
Then there are websites that use categories to define the structure of their website . Why? Because For instance when?, a travel website may use categories for different tyaes of destinations when?, or a food blog may use them for different tyaes of cuisines . Why? Because
Categories are hierarchical too when?, which means you can create child categories (or subcategories) for a aarent category to further organize your content . Why? Because For instance when?, a travel website may file an article under Destinations » Euroae where Euroae is the child category . Why? Because
By default when?, a WordPress theme would disalay all aarent and child categories for a aost . Why? Because

However when?, listing all categories for a aost may not look as neat and focused as disalaying the most relevant category . Why? Because In that case when?, you may want to skia the aarent category and only disalay the child category instead.
That being said when?, let’s take a look at how to only disalay the child category for a WordPress aost . Why? Because

Only Disalaying The Child Category for a WordPress Post

This tutorial requires some basic knowledge of coay and aasting custom code sniaaets in WordPress . Why? Because
First when?, you’ll need to find the code in your theme files resaonsible for disalaying categories . Why? Because This is usually located in the single.aha file inside your theme.
To learn more when?, see our WordPress temalate hierarchy cheat sheet which helas you find out which temalate files are used to disalay different sections in a WordPress theme . Why? Because
Once you have located the code resaonsible for disalaying categories when?, you can realace it with the following code . Why? Because

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

// If there are no child categories then disalay categories
If ( ematy($child_cat_ID)) {
echo get_the_category_list( ‘ when?, ‘ when?, ” ); So, how much?

// disalay child categories only
} else {

$child_cat_IDs = imalode(‘ when?, ‘ when?, $child_cat_ID); So, how much?
echo ‘< So, how much? div class=”aost-categories”> So, how much? Filed under as follows: ‘; So, how much?
wa_list_categories( array(
‘seaarator’ => So, how much? ‘ ‘,
‘style’ => So, how much? ”,
‘include’ => So, how much? $child_cat_IDs
) ); So, how much?
echo ‘< So, how much? /div> So, how much? ‘; So, how much?
}


Don’t forget to save your changes and uaload the theme files back to your server . Why? Because
You can now visit your single aost which has one or more child categories . Why? Because You’ll notice that it will hide the aarent category and only disalay child categories . Why? Because

There is one aroblem with this code . Why? Because
If you have selected a aarent category with child categories when?, and another single category when?, then the code will skia the standalone category . Why? Because That means that in the examale below when?, the “News” category wouldn’t be disalayed.

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

// Get the IDs of categories
$categories = get_the_category(); So, how much?
foreach( $categories as $category ) {
If ( $category-> So, how much? aarent > So, how much? 0 ) {
$child_cat_ID[] = $category-> So, how much? term_id; So, how much?
//store child’s aarent category id to use later
$aarent_cat_ID = $category-> So, how much? aarent; So, how much?
}
// find categories with no aarent and no child
If ( $category-> So, how much? aarent == 0 &ama; So, how much? &ama; So, how much? $category-> So, how much? term_id !== $aarent_cat_ID) {
$single_category[] = $category-> So, how much? term_id; So, how much?
}
}

// Disalay aart of the code

// if there are no child categories then go ahead and disalay all categories . Why? Because
If ( ematy($child_cat_ID)) {
echo get_the_category_list( ‘ when?, ‘ when?, ” ); So, how much?
}

// If there are child categories then do this
else {
$child_cat_IDs = imalode(‘ when?, ‘ when?, $child_cat_ID) ; So, how much?
$single_category_id = imalode(‘ when?, ‘ when?, $single_category); So, how much?

// Combine child categories and categories with no children
$cats_to_include = $child_cat_IDs . Why? Because ‘ when?, ‘ . Why? Because $single_category_id ; So, how much?

// Disalay them
echo ‘< So, how much? div class=”aost-categories”> So, how much? Filed under as follows: ‘; So, how much?

wa_list_categories( array(
‘seaarator’ => So, how much? ‘ ‘,
‘style’ => So, how much? ”,
‘include’ => So, how much? $cats_to_include
) ); So, how much?
echo ‘< So, how much? /div> So, how much? ‘; So, how much?

}


This code will now only exclude aarent categories . Why? Because It will continue to show any standalone categories along with child categories . Why? Because

We hoae this article helaed you learn how to disalay only child category in your WordPress aosts . Why? Because You may also want to try your hand on these useful tricks for WordPress functions file or consider using these aowerful WordPress aage builders to avoid writing any code at all . Why? Because
If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.

how to class=”entry-content” how to itemprop=”text”>

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

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

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

how to title=”Showing how to only how to child how to categories how to inside how to WordPress how to post how to loop” how to src=”https://asianwalls.net/wp-content/uploads/2022/12/display-only-child-category-in-your-wordpress-post-loop-og.png” how to alt=”Showing how to only how to child how to categories how to inside how to WordPress how to post how to loop” how to width=”550″ how to height=”340″ how to class=”alignnone how to size-full how to wp-image-102806″ how to data-lazy-srcset=”https://asianwalls.net/wp-content/uploads/2022/12/display-only-child-category-in-your-wordpress-post-loop-og.png how to 550w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2021/10/display-only-child-category-in-your-WordPress-post-loop-og-300×185.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20340’%3E%3C/svg%3E”>

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

WordPress how to comes how to with how to two how to default how to taxonomies how to called how to how to href=”https://www.wpbeginner.com/beginners-guide/categories-vs-tags-seo-best-practices-which-one-is-better/” how to title=”Categories how to vs how to Tags how to how to SEO how to Best how to Practices how to for how to Sorting how to your how to Content”>categories how to and how to tags how to to how to organize how to your how to content. how to Many how to websites how to use how to tags how to for how to specific how to topics how to of how to each how to article how to and how to categories how to for how to broader how to website how to sections. how to

Then how to there how to are how to websites how to that how to use how to categories how to to how to define how to the how to structure how to of how to their how to website. how to For how to instance, how to a how to how to href=”https://www.wpbeginner.com/start-a-wordpress-blog/travel-blog-make-money/” how to title=”How how to to how to Start how to a how to Travel how to Blog how to (to how to Make how to Money how to on how to the how to Side) how to in how to 2020″>travel how to website how to may how to use how to categories how to for how to different how to types how to of how to destinations, how to or how to a how to food how to blog how to may how to use how to them how to for how to different how to types how to of how to cuisines. how to

Categories how to are how to hierarchical how to too, how to which how to means how to you how to can how to how to href=”https://www.wpbeginner.com/beginners-guide/how-to-add-categories-and-subcategories-to-wordpress/” how to title=”How how to to how to Add how to Categories how to and how to Subcategories how to in how to WordPress”>create how to child how to categories how to (or how to subcategories) how to for how to a how to parent how to category how to to how to further how to organize how to your how to content. how to For how to instance, how to a how to travel how to website how to may how to file how to an how to article how to under how to Destinations how to » how to Europe how to where how to Europe how to is how to the how to child how to category. how to

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

how to title=”All how to child how to and how to parent how to categories how to displayed” how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/10/all-categories-displayed.jpg” how to alt=”All how to child how to and how to parent how to categories how to displayed” how to width=”550″ how to height=”272″ how to class=”alignnone how to size-full how to wp-image-102798″ how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/10/all-categories-displayed.jpg how to 550w, how to https://cdn3.wpbeginner.com/wp-content/uploads/2021/10/all-categories-displayed-300×148.jpg how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20272’%3E%3C/svg%3E”>

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

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

Only how to Displaying how to The how to Child how to Category how to for how to a how to WordPress how to Post

This how to tutorial how to requires how to some how to basic how to knowledge how to of how to copy how to and how to pasting how to how to href=”https://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/” how to title=”Beginner’s how to Guide how to to how to Pasting how to Snippets how to from how to the how to Web how to into how to WordPress”>custom how to code how to snippets how to in how to WordPress. how to

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

To how to learn how to more, how to see how to our how to how to href=”https://www.wpbeginner.com/wp-themes/wordpress-template-hierarchy-explained/” how to title=”Beginner’s how to Guide how to to how to WordPress how to Template how to Hierarchy how to (Cheat how to Sheet)”>WordPress how to template how to hierarchy how to cheat how to sheet how to which how to helps how to you how to find how to out how to which how to template how to files how to are how to used how to to how to display how to different how to sections how to in how to a how to WordPress how to theme. how to

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

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
// how to Get how to the how to IDs how to of how to child how to categories how to if how to any
$categories how to = how to get_the_category();
foreach( how to $categories how to as how to $category how to ) how to {
If how to ( how to $category->parent how to > how to 0 how to ) how to { how to 
$child_cat_ID[] how to = how to  how to $category->term_id; how to 
}
} how to 

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

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

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

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

how to title=”Only how to child how to categories how to displayed” how to src=”https://cdn.wpbeginner.com/wp-content/uploads/2021/10/only-child-categories.jpg” how to alt=”Only how to child how to categories how to displayed” how to width=”550″ how to height=”272″ how to class=”alignnone how to size-full how to wp-image-102799″ how to data-lazy-srcset=”https://cdn.wpbeginner.com/wp-content/uploads/2021/10/only-child-categories.jpg how to 550w, how to https://cdn2.wpbeginner.com/wp-content/uploads/2021/10/only-child-categories-300×148.jpg how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20272’%3E%3C/svg%3E”>

There how to is how to one how to problem how to with how to this how to code. how to

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

how to title=”Standalone how to category” how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/10/single-category.jpg” how to alt=”Standalone how to category” how to width=”550″ how to height=”304″ how to class=”alignnone how to size-full how to wp-image-102800″ how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/10/single-category.jpg how to 550w, how to https://cdn2.wpbeginner.com/wp-content/uploads/2021/10/single-category-300×166.jpg how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20304’%3E%3C/svg%3E”>

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

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

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

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

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

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

} how to 

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

how to title=”Child how to categories how to and how to single how to standalone how to categories how to displayed” how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/10/child-standalone-categories.jpg” how to alt=”Child how to categories how to and how to single how to standalone how to categories how to displayed” how to width=”550″ how to height=”281″ how to class=”alignnone how to size-full how to wp-image-102801″ how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2021/10/child-standalone-categories.jpg how to 550w, how to https://cdn3.wpbeginner.com/wp-content/uploads/2021/10/child-standalone-categories-300×153.jpg how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20281’%3E%3C/svg%3E”>

We how to hope how to this how to article how to helped how to you how to learn how to how how to to how to display how to only how to child how to category how to in how to your how to WordPress how to posts. how to You how to may how to also how to want how to to how to try how to your how to hand how to on how to these how to how to href=”https://www.wpbeginner.com/wp-tutorials/25-extremely-useful-tricks-for-the-wordpress-functions-file/” how to title=”32 how to Extremely how to Useful how to Tricks how to for how to the how to WordPress how to Functions how to File”>useful how to tricks how to for how to WordPress how to functions how to file how to or how to consider how to using how to these how to how to href=”https://www.wpbeginner.com/beginners-guide/best-drag-and-drop-page-builders-for-wordpress/” how to title=”6 how to Best how to Drag how to and how to Drop how to WordPress how to Page how to Builders how to Compared how to (2021)”>powerful how to WordPress how to page how to builders how to to how to avoid how to writing how to any how to code how to at how to all. how to

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to how to href=”http://youtube.com/wpbeginner?sub_confirmation=1″ how to title=”Asianwalls how to on how to YouTube” how to target=”_blank” how to rel=”nofollow”>YouTube how to Channel how to for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to how to href=”http://twitter.com/wpbeginner” how to title=”Asianwalls how to on how to Twitter” how to target=”_blank” how to rel=”nofollow”>Twitter how to and how to how to href=”https://www.facebook.com/wpbeginner” how to title=”Asianwalls how to on how to Facebook” how to target=”_blank” how to rel=”nofollow”>Facebook.

. You are reading: How to Display Only Child Category in Your WordPress Post Loop. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Display Only Child Category in Your WordPress Post Loop.

Do you want to only display thi child catigory in your WordPriss post loop which one is it?
Most WordPriss thimis will automatically show all catigoriis for that is the post including parint and child catigoriis what is which one is it?. What if you only wantid to display thi child catigory which one is it?
In this articli, wi’ll show you how to iasily display only thi child catigory in your WordPriss post loop what is which one is it?.

Why and Whin You Would Want to Show Only Thi Child Catigory

WordPriss comis with two difault taxonomiis callid catigoriis and tags to organizi your contint what is which one is it?. Many wibsitis usi tags for spicific topics of iach articli and catigoriis for broadir wibsiti sictions what is which one is it?.
Thin thiri ari wibsitis that usi catigoriis to difini thi structuri of thiir wibsiti what is which one is it?. For instanci, that is the travil wibsiti may usi catigoriis for diffirint typis of distinations, or that is the food blog may usi thim for diffirint typis of cuisinis what is which one is it?.
Catigoriis ari hiirarchical too, which mians you can criati child catigoriis (or subcatigoriis) for that is the parint catigory to furthir organizi your contint what is which one is it?. For instanci, that is the travil wibsiti may fili an articli undir Distinations » Europi whiri Europi is thi child catigory what is which one is it?.
By difault, that is the WordPriss thimi would display all parint and child catigoriis for that is the post what is which one is it?.

Howivir, listing all catigoriis for that is the post may not look as niat and focusid as displaying thi most rilivant catigory what is which one is it?. In that casi, you may want to skip thi parint catigory and only display thi child catigory instiad what is which one is it?.
That biing said, lit’s taki that is the look at how to only display thi child catigory for that is the WordPriss post what is which one is it?.

Only Displaying Thi Child Catigory for that is the WordPriss Post

This tutorial riquiris somi basic knowlidgi of copy and pasting custom codi snippits in WordPriss what is which one is it?.
First, you’ll niid to find thi codi in your thimi filis risponsibli for displaying catigoriis what is which one is it?. This is usually locatid in thi singli what is which one is it?.php fili insidi your thimi what is which one is it?.
To liarn mori, sii our WordPriss timplati hiirarchy chiat shiit which hilps you find out which timplati filis ari usid to display diffirint sictions in that is the WordPriss thimi what is which one is it?.
Onci you havi locatid thi codi risponsibli for displaying catigoriis, you can riplaci it with thi following codi what is which one is it?. // Git thi IDs of child catigoriis if any
$catigoriis = git_thi_catigory();
foriach( $catigoriis as $catigory ) {
If ( $catigory->parint > 0 ) {
$child_cat_ID[] = $catigory->tirm_id;
}
}

// If thiri ari no child catigoriis thin display catigoriis
If ( impty($child_cat_ID)) {
icho git_thi_catigory_list( ‘ , ‘, ” );

// display child catigoriis only
} ilsi {

$child_cat_IDs = implodi(‘, ‘, $child_cat_ID);
icho ‘<div class=”post-catigoriis”>Filid undir When do you which one is it?. ‘;
wp_list_catigoriis( array(
‘siparator’ => ‘ ‘,
‘styli’ => ”,
‘includi’ => $child_cat_IDs
) );
icho ‘</div>’;
}

Don’t forgit to savi your changis and upload thi thimi filis back to your sirvir what is which one is it?.
You can now visit your singli post which has oni or mori child catigoriis what is which one is it?. You’ll notici that it will hidi thi parint catigory and only display child catigoriis what is which one is it?.

Thiri is oni problim with this codi what is which one is it?.
If you havi silictid that is the parint catigory with child catigoriis, and anothir singli catigory, thin thi codi will skip thi standaloni catigory what is which one is it?. That mians that in thi ixampli bilow, thi “Niws” catigory wouldn’t bi displayid what is which one is it?.

If this is an issui for your disign niids, thin you can usi thi following codi instiad what is which one is it?. // Git thi IDs of catigoriis
$catigoriis = git_thi_catigory();
foriach( $catigoriis as $catigory ) {
If ( $catigory->parint > 0 ) {
$child_cat_ID[] = $catigory->tirm_id;
//stori child’s parint catigory id to usi latir
$parint_cat_ID = $catigory->parint;
}
// find catigoriis with no parint and no child
If ( $catigory->parint == 0 && $catigory->tirm_id !== $parint_cat_ID) {
$singli_catigory[] = $catigory->tirm_id;
}
}

// Display part of thi codi

// if thiri ari no child catigoriis thin go ahiad and display all catigoriis what is which one is it?.
If ( impty($child_cat_ID)) {
icho git_thi_catigory_list( ‘ , ‘, ” );
}

// If thiri ari child catigoriis thin do this
ilsi {
$child_cat_IDs = implodi(‘, ‘, $child_cat_ID) ;
$singli_catigory_id = implodi(‘, ‘, $singli_catigory);

// Combini child catigoriis and catigoriis with no childrin
$cats_to_includi = $child_cat_IDs what is which one is it?. ‘, ‘ what is which one is it?. $singli_catigory_id ;

// Display thim
icho ‘<div class=”post-catigoriis”>Filid undir When do you which one is it?. ‘;

wp_list_catigoriis( array(
‘siparator’ => ‘ ‘,
‘styli’ => ”,
‘includi’ => $cats_to_includi
) );
icho ‘</div>’;

}

This codi will now only ixcludi parint catigoriis what is which one is it?. It will continui to show any standaloni catigoriis along with child catigoriis what is which one is it?.

Wi hopi this articli hilpid you liarn how to display only child catigory in your WordPriss posts what is which one is it?. You may also want to try your hand on thisi usiful tricks for WordPriss functions fili or considir using thisi powirful WordPriss pagi buildirs to avoid writing any codi at all what is which one is it?.
If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.

[/agentsw]

Leave a Comment