Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To keep the posts in a WordPress loop but show only child categories beside each post, get that post’s assigned categories, filter the category objects, and print links for the ones that have a parent. This displays direct child categories; it does not change which posts the loop returns.

Filter the categories shown for each post

Use get_the_category() inside the loop to retrieve the current post’s assigned categories. A direct child category has a nonzero parent term ID. The following example keeps the loop’s posts unchanged and outputs a list only when the post has at least one direct child category:

<?php
while ( have_posts() ) :
    the_post();

    $categories = get_the_category();
    $child_categories = array_filter(
        $categories,
        static function ( $category ) {
            return 0 !== (int) $category->parent;
        }
    );

    if ( $child_categories ) {
        echo '<ul class="post-child-categories">';
        foreach ( $child_categories as $category ) {
            printf(
                '<li><a href="%1$s">%2$s</a></li>',
                esc_url( get_category_link( $category->term_id ) ),
                esc_html( $category->name )
            );
        }
        echo '</ul>';
    }
endwhile;
?>

The parent check selects direct children anywhere in the category tree. Because it checks only whether a parent exists, it can include categories from multiple branches. The category and term-query references explain category retrieval and term hierarchy: get_categories() and WP_Term_Query.

Limit output to one category branch

If “child categories” means descendants of one particular parent, a nonzero parent check is too broad. Filter against the intended parent’s hierarchy instead; this distinguishes descendants in that branch from child categories elsewhere. Also decide whether “child” means immediate children only or includes grandchildren and deeper descendants. The WP_Term_Query reference covers the term-query options for working with hierarchical terms.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Display filtering is different from filtering posts

The code above controls category links printed for each post. It does not change the posts returned by the main loop. If the goal is instead to show only posts filed under a category, adjust the query—for example, with WP_Query category arguments or a taxonomy query.

Choose the query argument based on whether posts in descendant categories should qualify. WordPress documents that cat and category_name include descendants of the selected category, while category__in does not. A taxonomy query can specify include_children for a hierarchical taxonomy. See the WP_Query reference for the documented behavior.

Choose the right function for a category list

For categories assigned to each post, retrieve that post’s terms and filter them as shown above. For a standalone list of category terms, use get_categories() when you need category objects for custom filtering or markup. Its parent argument asks for immediate children of a term; child_of scopes the results to descendants. The hide_empty argument controls whether terms with no posts appear. If WordPress’s generated list markup suits the template, wp_list_categories() is an alternative. See get_categories() and wp_list_categories().

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Filter categories through a hook

If you need to modify the categories returned when another function retrieves a post’s categories, WordPress provides the get_the_categories filter, which passes the post ID. That changes the returned category data; use it only when that broader behavior is intended. The hook is documented at get_the_categories.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the result in your theme

  • Confirm whether the desired output is category links for each post or a loop restricted to certain posts.
  • Decide whether to show immediate children only or all descendants of a selected parent.
  • Test against the site’s actual category hierarchy and WordPress version; the code example is not a claim of compatibility testing.
  • Choose custom markup when you need to filter post categories, or generated markup for a standalone list when wp_list_categories() already fits.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.