Display Only the Child Category

How to Display Only the Child Category in Your WordPress Post Loop

WordPress categories help you group and organize your content. Often, you’ll have a parent category for a broad topic and child categories for more specific subtopics.

For example:

  • TechnologyArtificial Intelligence
  • TravelParis

By default, WordPress shows all assigned categories for a post.

Categories

But sometimes, you may want to show only the child category — the most specific topic — in your post loop or single post view. This keeps your display clean and makes it easier for readers to understand exactly what the post is about.

Why Show Only the Child Category?

Here are some reasons you might want to display only child categories:

  • Cleaner look – Avoid listing both parent and child categories.
  • Highlight specificity – Show readers the exact subtopic instead of broad topics.
  • Better UX – Readers can quickly find related posts in that exact subtopic.

Step 1: Add the Custom Function

We’ll create a PHP function that filters assigned categories and returns only those that are not top-level (i.e., parent != 0).

Add this code to your theme’s functions.php file.

if ( ! function_exists( 'wpp_get_child_terms' ) ) {
    function wpp_get_child_terms( $post_id = 0, $taxonomy = 'category' ) {
        if ( ! $post_id ) {
            $post_id = get_the_ID();
        }
        if ( ! $post_id ) {
            return '';
        }
        $terms = get_the_terms( $post_id, $taxonomy );
        if ( empty( $terms ) || is_wp_error( $terms ) ) {
            return '';
        }
        $child_terms = array_filter( $terms, function( $term ) {
            return $term->parent != 0;
        } );

        if ( empty( $child_terms ) ) {
            return '';
        }
        $links = array();
        foreach ( $child_terms as $term ) {
            $link = get_term_link( $term );
            if ( is_wp_error( $link ) ) {
                continue;
            }
            $links[] = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $term->name ) . '">' . esc_html( $term->name ) . '</a>';
        }
        return implode( ', ', $links );
    }
}

Step 2: Replace the Default Category Output

In your theme’s template (e.g., content.php, single.php, or archive.php), look for:

<?php the_category(); ?>

Replace it with:

<?php echo wpp_get_child_terms(); ?>

This will now output only the child categories assigned to each post.

Child Category

Step 3: Using Other Taxonomies

Want to use the same logic for another taxonomy, such as WooCommerce product categories?

Example:

<?php echo wpp_get_child_terms( 0, 'product_cat' ); ?>

Step 4: Testing

  • Assign both a parent and child category to a post — only the child category should display.
  • Assign only a parent category — nothing will display unless you add a fallback (optional).

Final Thoughts

Showing only child categories is a simple way to make your category display more precise and less cluttered. This method works for default post categories, WooCommerce product categories, or any custom taxonomy. By focusing on the most specific term, you improve clarity for your readers and create a cleaner post presentation.

Leave a Reply

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