BlogThemes

How to Display Only Child Category in your WordPress Post Loop

In our earlier publish, we shared how one can solely show dad or mum class in your WordPress Publish Loop. This text is the continuation of the same approach besides right here we’ll attempt to solely present baby class in your WordPress loop. Sadly the_category() doesn’t have any non-obligatory parameters like baby=zero or depth=-1. When creating our WordPress Gallery, this was one of many points we needed to take care of to prepare our single web site pages the best way we wished.

To show solely Youngster Class within the publish loop (largely single.php), all it’s important to do is exchange the next code:

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

with this code:

1
2
3
4
5
6
7
<?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>';
}}
?>

Keep in mind to vary the quantity 10 to your dad or mum class’s ID.

In our gallery’s case, we had the dad or mum class known as Theme Framework, and bunch of kid classes. Every publish was solely going to be assigned one baby class for the framework (for instance Genesis). So this code labored out completely. See the dwell instance by clicking the picture beneath:

Display Only Parent Category in Your WordPress Loop

Hope this trick solves your downside as properly.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBest WordPress PluginsBlog

9 Best Social Proof Plugins for WordPress & WooCommerce (2020)

BlogTutorials

How To Add Pagination To Your WordPress Theme

Tips

9 Things You Should Consider while Choosing a WordPress Theme

Tips

The Dilemma Between “Pretty” And User Friendly: How To Pick The Perfect WordPress Theme

Leave a Reply

Your email address will not be published.

Pay in your preferred currency
Indian rupee
Euro