Display Only Parent or Only Child Categories

How to Display Only Parent or Only Child Categories in Your WordPress Post Loop

Previously we wrote two articles on how to:

  1. Show Only the Parent Category in Your WordPress Post Loop
  2. Display Only the Child Category in Your WordPress Post Loop

In this tutorial, we will give you a combined function that lets you choose "parent" or "child" output with just one call, instead of having two separate functions. That way, you can reuse the same code for both cases.

By default, WordPress will display all categories assigned to a post.

Categories

That’s fine in many cases, but sometimes you want more control.

Maybe you want to show only the main (parent) category to keep things clean, or perhaps you want to show only the child category to highlight the most specific topic. In this guide, we’ll cover both — using one easy, reusable function.

Why Filter Categories?

Here are some real-world scenarios where filtering categories makes sense:

  • Show only parent categories if you want to emphasize broad topics without clutter.
  • Show only child categories if you want to highlight specific subtopics.
  • Apply it to custom taxonomies like WooCommerce product categories, genres, or locations.

Example category structure:

  • TechnologyArtificial Intelligence
  • TravelParis
  • RecipesDessert

If a post is assigned to Technology and Artificial Intelligence, you can choose to display only “Technology” (parent) or only “Artificial Intelligence” (child).

Step 1: Add the Flexible Function

We’ll create one function that can return either parent or child terms based on a parameter. Add this to your theme’s functions.php file.

if ( ! function_exists( 'wpp_get_terms_by_type' ) ) {
    function wpp_get_terms_by_type( $post_id = 0, $taxonomy = 'category', $type = 'parent' ) {
        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 '';
        }
        $filtered_terms = array();
        if ( strtolower( $type ) === 'parent' ) {
            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;
                }
                $filtered_terms[ intval( $top->term_id ) ] = $top;
            }
        } elseif ( strtolower( $type ) === 'child' ) {
            $filtered_terms = array_filter( $terms, function( $term ) {
                return $term->parent != 0;
            } );
        } else {
            return '';
        }
        if ( empty( $filtered_terms ) ) {
            return '';
        }
        $links = array();
        foreach ( $filtered_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: How to Use the Function

The function takes three parameters:

  1. Post ID – Use 0 to get the current post in the loop.
  2. Taxonomy slug – For normal categories, use 'category'.
  3. Type – Either 'parent' or 'child'.

Example: Show Only Parent Category

<?php echo wpp_get_terms_by_type( 0, 'category', 'parent' ); ?>

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

Parent Category

Example: Show Only Child Category

<?php echo wpp_get_terms_by_type( 0, 'category', 'child' ); ?>

Now, only child categories appear where previously all categories were shown.

Child Category

Step 3: Using It with WooCommerce

You can also use it with product categories in WooCommerce.

Show only parent product categories:

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

Show only child product categories:

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

Step 4: Replacing the_category()

Most themes display categories with:

<?php the_category(); ?>

Replace that with your preferred function call:

<?php echo wpp_get_terms_by_type( 0, 'category', 'parent' ); ?>

or

<?php echo wpp_get_terms_by_type( 0, 'category', 'child' ); ?>

Step 5: Testing

  1. Assign a post to both a parent and a child category.
  2. Use the 'parent' type to see only the parent.
  3. Switch to 'child' type to see only the child.
  4. For posts with only one category, the function will show only that term if it matches the chosen type.

Final Thoughts

This approach keeps your post loops and single post views clean and relevant. Whether you want to emphasize broad topics with parent categories or highlight specific subtopics with child categories, you can now do it with a single function.

You can even reuse it for custom taxonomies — making it a versatile tool for almost any WordPress project.

Leave a Reply

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