display only parent category in your WordPress post loop og 1

Do you want your WordPress posts to just show the parent category?

Most WordPress themes list all categories linked with a post by default. Some users, on the other hand, may only want to see the parent category and not the child categories.

Advertisements

We’ll show you how to change the WordPress post loop so that only the parent category is displayed on a single post in this article.

Displaying only the parent category in WordPress loop

When should you just show the parent category in WordPress?

Many website owners utilize parent and child categories to organize their sites.

For example, a travel blog might categorize vacation destinations into categories, with each area serving as the parent category and cities serving as the child categories.

Parent and child categories displayed

A food blog, for example, might feature recipes divided into parent and child categories. A parent category might be cuisine type, whereas a child category might be dish type.

The category() template tag is now used by most WordPress themes to list all categories linked with a post.

 

This tool is useful, but it displays all categories in alphabetical order and ignores the parent/child relationship entirely.

 

That being said, let’s look at how to modify this behavior so that the WordPress loop only shows the parent category.

In the WordPress Post Loop, just the parent category is displayed.

You’ll need to edit your WordPress theme files for this guide. Check out our post on how to copy and paste code snippets in WordPress if you haven’t done so before.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 just creates the wpb get parent terms function (). This function will only show parent categories by default.

Then, in the WordPress theme files where you wish to display the parent category alone, include this function.

See our WordPress template hierarchy cheatsheet for novices to figure out which template file to look into.

Within the WordPress loop, you’ll be looking for the category(); template tag. Once you’ve located it, you’ll need to replace it with the code below:

1
<?php wpb_get_parent_terms(); ?>

 

This code will only show the parent category. If you have numerous parent or independent categories, then all of them will be displayed.

Only parent category displayed

This code will also work for other taxonomies. For example, WooCommerce product categories or any other custom taxonomy you might have.

Simply change the code to look like this:

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

 

This code will show product categories in a WooCommerce store, but only the parent or standalone categories for each product.

Only parent product category displayed

If you want to show a custom taxonomy, replace product cat with the name of the custom taxonomy.

We hope that this article has shown you how to just show the parent category for your WordPress posts. You might also be interested in our guide to creating custom WordPress pages without writing code or these other WordPress category hacks and tricks.

Advertisements

Leave a Reply