Exclude parent categories from recent posts list

I want to show only the sub-categories from a post’s specific parent category and exclude all the other categories.

This is for a loop of recent posts on my front-page.php. Each recent post is currently listing its categories using get_the_category_list($post->ID).

I tried get_the_category_list($parent, ‘8’) but it didn’t exclude anything. 8 is the integer of the parent category I want to keep. Any help is much appreciated!

Here’s my code:

<?php $the_query = new WP_Query(array(
‘post_type’ => ‘post’,
‘category_name’ => ‘genres’,
‘posts_per_page’ => 6,
‘post_status’ => ‘publish’

while ( $the_query->have_posts() ) : $the_query->the_post();

echo ‘<div class=”wrapper”>
<div> // stuff with post title, image and excerpt </div>
<div>’ . get_the_category_list( $post->ID ) . ‘</div>’;

wp_reset_postdata(); ?>

I want to go from this:

Parent-category 1, Sub-category 1 , Parent-category 2, Sub-category 2, Parent-category 3, Sub-category 3

To this:

Sub-category 1

Read more here:: Exclude parent categories from recent posts list

Leave a Reply

Your email address will not be published. Required fields are marked *