Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Display Only Parent Category in the WordPress Post Loop

Do you want to display only the parent category for your WordPress posts?

By default, most WordPress themes will list all categories associated with a post. However, some users may only want to display the parent category instead, and exclude child categories.

In this article, we’ll show you how to modify the WordPress post loop to display only the parent category on a single post.

Displaying only the parent category in WordPress loop

When to Display Only Parent Category in WordPress

Many website owners use parent and child categories to create a site structure for their websites.

For instance, a travel blog may have travel destinations organized by categories where each region is a parent category and cities as child categories.

Parent and child categories displayed

Similarly, a food blog may publish recipes organized in parent and child categories. For instance, a parent category could be cuisine type and a child category could be dish type.

Now, most WordPress themes use the_category() template tag to list all categories associated with a post.

This function is efficient, but it will display all categories alphabetically and ignore the parent/child relationship completely.

That being said, let’s take a look at how to change this behavior and only display the parent category in the WordPress loop.

Display Only Parent Category in WordPress Post Loop

For this tutorial, you’ll need to edit your WordPress theme files. If you haven’t done this before then check out our article on how to copy and paste code snippets in WordPress.

First, you need to add the following code to your theme’s functions.php file or a site-specific plugin.

function wpb_get_parent_terms($taxonomy = 'category')
{
    $currentPost = get_post();
    $terms       = get_the_terms($currentPost->ID, $taxonomy);

    if (is_wp_error($terms)) {
        /** @var \WP_Error $terms */
        throw new \Exception($terms->get_error_message());
    }

    $map = array_map(
        function ($term) use ($taxonomy) {
            return '<a href="' . esc_url(get_term_link($term->term_id,
                    $taxonomy)) . '" title="' . esc_attr($term->name) . '">
                ' . $term->name . '
                </a>';
        },
        array_filter($terms, function ($term) {
            return $term->parent == 0;
        })
    );

    return implode(', ', $map);
}

This code simply creates a new function wpb_get_parent_terms(). By default, this function will only display parent categories.

Next, you need to place this function in your WordPress theme files where you want to display the parent category alone.

To figure out which template file to look into, see our WordPress template hierarchy cheatsheet for beginners.

Basically, you will be looking for the_category(); template tag inside the WordPress loop. Once you have found it, you need to replace it with the following code:

<?php wpb_get_parent_terms(); ?> 

This code will display your parent category alone. If you have multiple categories that are the parent or standalone categories, then all such categories will also be displayed.

Only parent category displayed

This code will work for all other taxonomies as well. For instance, WooCommerce product categories or any custom taxonomy that you may have.

Simply modify the code like this:

<?php wpb_get_parent_terms( 'product_cat '); ?>

This code will display product categories for a WooCommerce store and will only display the parent or standalone categories for a product.

Only parent product category displayed

If you want to display a custom taxonomy, then replace product_cat with your custom taxonomy name.

We hope this article helped you learn how to display only parent category for your WordPress posts. You may also want to see these other WordPress category hacks and tips or see our guide on creating custom WordPress pages without writing code.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

11 CommentsLeave a Reply

  1. put date stamps to your posts. this way we know when you wrote it and not include code snippets 5 years old.

Leave a Reply to sathish Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.