Tuesday, February 19, 2019

Display Only Child Category in WordPress Post Loop

To display only Child Category in the post loop (mostly single.php), all you have to do is replace the following 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(9, $childcat)) {
echo '<a href="'.get_category_link($childcat->cat_ID).'">';
 echo $childcat->cat_name . '</a>';
}}
?>
Remember to change the number 9 to your parent category’s ID.

No comments:

Post a Comment

How to Hide Elements on Specific WordPress Pages

Find the post or page id. something like: .postid-1762 or .pageid-1762. Then add to your css : .postid-1762 #your-element { display :...