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 will show you how to modify the WordPress post loop to display only the parent category on a single post.

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.

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 the following code to your theme’s functions.php file or use a code snippets plugin such as WPCode (recommended):
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);
}
For more details, see our guide on how to add custom code in WordPress without breaking your site.
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.

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 and will only display the parent or standalone categories for a product.

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 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.
Davide says
Great! And if i want to give a different css to these categories?
Pedrille says
Awesome ! thks
Nithi says
How to display only Child category? (Like in the image, “Thesis” above? )
Cami Tirapani says
I’m wondering the same thing. :/
Joseph says
I know its old, but that would just be the current category
sathish says
very useful
JordashTalon says
You should vote this post up on WordPress I’ve noticed a huge lack of adding the Depth feature to WordPress Functions: http://wordpress.org/extend/ideas/topic/add-depth-parameter-to-the_category?replies=3#post-21041
JordashTalon says
WordPress Category functions that is
Kate M says
Didn’t work for me
AselHora says
put date stamps to your posts. this way we know when you wrote it and not include code snippets 5 years old.
John Saddington says
great tut!