How to Show Only the Parent Category in Your WordPress Post Loop

How to Show Only the Parent Category in Your WordPress Post Loop

Categories and subcategories help structure your content and improve navigation—but cluttering your posts with every single category can overwhelm readers. What if you could simplify things and display only the main (parent) category on your post pages? It’s easy—and here’s how!

Why Display Only the Parent Category?

Many sites organize content with parent and child categories—for example:

  • In a travel blog: EuropeParis
  • In a recipe site: Cuisine TypeDessert

By default, WordPress’s the_category() function will list all assigned categories, mixing parents and children without hierarchy.

Categories

That’s where this tweak comes in: spotlight the core topic by showing just the parent category.

Step-by-Step Instructions

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

if ( ! function_exists( 'wpp_get_parent_terms' ) ) {
    function wpp_get_parent_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 '';
        }
        $parents = array();
        foreach ( $terms as $term ) {
            $top = $term;
            while ( ! empty( $top->parent ) ) {
                $ancestor = get_term( $top->parent, $taxonomy );
                if ( is_wp_error( $ancestor ) || ! $ancestor ) {
                    break;
                }
                $top = $ancestor;
            }
            $parents[ intval( $top->term_id ) ] = $top;
        }
        if ( empty( $parents ) ) {
            return '';
        }
        $links = array();
        foreach ( $parents as $parent ) {
            $link = get_term_link( $parent );
            if ( is_wp_error( $link ) ) {
                continue;
            }
            $links[] = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $parent->name ) . '">' . esc_html( $parent->name ) . '</a>';
        }
        return implode( ', ', $links );
    }
}

Locate where your theme uses the_category() (often in single post templates) and replace it with:

<?php echo wpp_get_parent_terms(); ?>

Now, only parent or standalone categories appear where previously all categories were shown.

Parent Category

Want to apply the same logic to other taxonomies—like WooCommerce product categories or your own custom sets? Just call the function with the taxonomy name:

<?php echo wpp_get_parent_terms( 0, 'product_cat' ); ?>
Product Parent Category

In Summary

  • Use the custom wpp_get_parent_terms() function to filter out child categories.
  • Display just the parent category by replacing the_category() with the function call.
  • Applies to categories, product categories, or any taxonomy via the parameter.

Leave a Reply

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