Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Display Only Parent Category in the WordPress Post Loop

At WPBeginner we understand that creating a well-structured hierarchy of categories and subcategories can improve user experience and SEO.

By default, most WordPress themes will list all categories associated with a post.

However, one of our readers asked if it is possible to display only the parent category within the post loop and exclude child categories. They want to focus their visitors’ attention on the broader topic of each post instead of a complex hierarchy.

In this article, we’ll show you how to use a code snippet to modify the WordPress post loop and 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 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 will 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 a code to your theme’s functions.php file or use a code snippets plugin. We recommend using WPCode because it lets you add code without breaking your site, and its smart code snippet validation helps you prevent common code errors.

Learn more in our guide on how to add custom code in WordPress without breaking your site.

For this tutorial, we’ll use the free version of WPCode. While it doesn’t have all of the advanced features of the full version, it comes with everything you need to get started.

The first thing you need to do is install and activate the WPCode plugin. If you need help, then you can follow our step-by-step guide on how to install a WordPress plugin.

Upon activation, you can go to the WPCode » New Snippet page

Adding a New Snippet in WPCode

This will open a page where you can create a custom snippet.

First, you need to add a title for the snippet. After that, you can copy and paste the following code snippet into the Code Preview pane.

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);
}
Adding a Title and Code Snippet to WPCode

Next, you need to select the ‘PHP Snippet’ option from the Code Type drop-down menu and toggle the Inactive switch to Active.

Finally, make sure you click the ‘Save Snippet’ button to store your settings.

In WPCode, Select PHP as the Code Type and Toggle the Snippet Active

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

The code snippet 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.

It will also 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.

Expert Guides on Displaying Categories in WordPress

Now that you know how to display only the parent category for your WordPress posts, you may like to see some other guides related to displaying categories in WordPress:

We hope this article helped you learn how to display only the parent category for your WordPress posts. You may also want to see our guide on how to display custom fields outside the loop or our expert pick of the best AI chatbot software for your website.

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. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

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

12 CommentsLeave a Reply

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Davide

    Great! And if i want to give a different css to these categories?

  3. Pedrille

    Awesome ! thks

  4. Nithi

    How to display only Child category? (Like in the image, “Thesis” above? )

    • Cami Tirapani

      I’m wondering the same thing. :/

      • Joseph

        I know its old, but that would just be the current category

  5. sathish

    very useful

  6. Kate M

    Didn’t work for me :(

  7. AselHora

    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

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.