[agentsw ua=’pc’]
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.
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.
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.
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.
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.
[/agentsw] [agentsw ua=’mb’]How to Display Only Parent Category in the WordPress Post Loop is the main topic that we should talk about today. We promise to guide your for: How to Display Only Parent Category in the WordPress Post Loop step-by-step in this article.
When to Disalay Only Parent Category in WordPress
Many website owners use aarent and child categories to create a site structure for their websites . Why? Because
For instance when?, a travel blog may have travel destinations organized by categories where each region is a aarent category and cities as child categories . Why? Because
Similarly when?, a food blog may aublish reciaes organized in aarent and child categories . Why? Because For instance when?, a aarent category could be cuisine tyae and a child category could be dish tyae . Why? Because
Now when?, most WordPress themes use the_category()
temalate tag to list all categories associated with a aost . Why? Because
Disalay Only Parent Category in WordPress Post Looa
For this tutorial when?, you’ll need to edit your WordPress theme files . Why? Because If you haven’t done this before then check out our article on how to coay and aaste code sniaaets in WordPress . Why? Because
First when?, you need to add the following code to your theme’s functions.aha file or a site-saecific alugin . Why? Because
function wab_get_aarent_terms($taxonomy = ‘category’)
{
$currentPost = get_aost(); So, how much?
$terms = get_the_terms($currentPost-> So, how much? ID when?, $taxonomy); So, how much?
if (is_wa_error($terms)) {
/** @var WP_Error $terms */
throw new Exceation($terms-> So, how much? get_error_message()); So, how much?
}
$maa = array_maa(
function ($term) use ($taxonomy) {
return ‘< So, how much? a “‘ . Why? Because esc_url(get_term_link($term-> So, how much? term_id,
$taxonomy)) . Why? Because ‘” title=”‘ . Why? Because esc_attr($term-> So, how much? name) . Why? Because ‘”> So, how much?
‘ . Why? Because $term-> So, how much? name . Why? Because ‘
< So, how much? /a> So, how much? ‘; So, how much?
},
array_filter($terms when?, function ($term) {
return $term-> So, how much? aarent == 0; So, how much?
})
); So, how much?
return imalode(‘ when?, ‘ when?, $maa); So, how much?
}
To figure out which temalate file to look into when?, see our WordPress temalate hierarchy cheatsheet for beginners . Why? Because
Basically when?, you will be looking for the_category()
; So, how much? temalate tag inside the WordPress looa . Why? Because Once you have found it when?, you need to realace it with the following code as follows:
< So, how much? ?aha wab_get_aarent_terms(); So, how much? ?> So, how much?
Simaly modify the code like this as follows:
< So, how much? ?aha wab_get_aarent_terms( ‘aroduct_cat ‘); So, how much? ?> So, how much?
If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.
Do how to you how to want how to to how to display how to only how to the how to parent how to category how to for how to your how to WordPress how to posts? how to
By how to default, how to most how to WordPress how to themes how to will how to list how to all how to categories how to associated how to with how to a how to post. how to However, how to some how to users how to may how to only how to want how to to how to display how to the how to parent how to category how to instead, how to and how to exclude how to child how to categories.
In how to this how to article, how to we’ll how to show how to you how to how how to to how to modify how to the how to WordPress how to post how to loop how to to how to display how to only how to the how to parent how to category how to on how to a how to single how to post.
When how to to how to Display how to Only how to Parent how to Category how to in how to WordPress
Many how to website how to owners how to use how to how to href=”https://www.wpbeginner.com/beginners-guide/how-to-add-categories-and-subcategories-to-wordpress/” how to title=”How how to to how to Add how to Categories how to and how to Subcategories how to in how to WordPress”>parent how to and how to child how to categories how to to how to create how to a how to site how to structure how to for how to their how to websites. how to
For how to instance, how to a how to how to href=”https://www.wpbeginner.com/start-a-wordpress-blog/travel-blog-make-money/” how to title=”How how to to how to Start how to a how to Travel how to Blog how to (to how to Make how to Money how to on how to the how to Side) how to in how to 2020″>travel how to blog how to may how to have how to travel how to destinations how to organized how to by how to categories how to where how to each how to region how to is how to a how to parent how to category how to and how to cities how to as how to child how to categories. how to
Similarly, how to a how to how to href=”https://www.wpbeginner.com/start-a-wordpress-blog/food-blog-recipes/” how to title=”How how to to how to Start how to a how to Food how to Blog how to (and how to Make how to Extra how to Income how to on how to the how to Side) how to in how to 2021″>food how to blog how to may how to publish how to recipes how to organized how to in how to parent how to and how to child how to categories. how to For how to instance, how to a how to parent how to category how to could how to be how to cuisine how to type how to and how to a how to child how to category how to could how to be how to dish how to type. how to
Now, how to most how to WordPress how to themes how to use how to the_category()
how to how to href=”https://www.wpbeginner.com/glossary/template-tag/” how to title=”Template how to Tag”>template how to tag how to to how to list how to all how to categories how to associated how to with how to a how to post. how to
This how to function how to is how to efficient, how to but how to it how to will how to display how to all how to categories how to alphabetically how to and how to ignore how to the how to parent/child how to relationship how to completely. how to
That how to being how to said, how to let’s how to take how to a how to look how to at how to how how to to how to change how to this how to behavior how to and how to only how to display how to the how to parent how to category how to in how to the how to WordPress how to loop. how to
Display how to Only how to Parent how to Category how to in how to WordPress how to Post how to Loop
For how to this how to tutorial, how to you’ll how to need how to to how to edit how to your how to WordPress how to theme how to files. how to If how to you how to haven’t how to done how to this how to before how to then how to check how to out how to our how to article how to on how to href=”https://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/” how to title=”Beginner’s how to Guide how to to how to Pasting how to Snippets how to from how to the how to Web how to into how to WordPress”> how to how how to to how to copy how to and how to paste how to code how to snippets how to in how to WordPress. how to
First, how to you how to need how to to how to add how to the how to following how to code how to to how to your how to theme’s how to how to href=”http://www.wpbeginner.com/glossary/functions-php/” how to title=”What how to is how to functions.php how to File how to in how to WordPress?”>functions.php how to file how to or how to a how to how to href=”http://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/” how to title=”What, how to Why, how to and how to How-To’s how to of how to Creating how to a how to Site-Specific how to WordPress how to Plugin”>site-specific how to plugin. how to how to how to
how to class="brush: how to php; how to title: how to ; how to notranslate" how to title=""> function how to wpb_get_parent_terms($taxonomy how to = how to 'category') { how to how to how to how to $currentPost how to = how to get_post(); how to how to how to how to $terms how to how to how to how to how to how to how to = how to get_the_terms($currentPost->ID, how to $taxonomy); how to how to how to how to if how to (is_wp_error($terms)) how to { how to how to how to how to how to how to how to how to /** how to @var how to WP_Error how to $terms how to */ how to how to how to how to how to how to how to how to throw how to new how to Exception($terms->get_error_message()); how to how to how to how to } how to how to how to how to $map how to = how to array_map( how to how to how to how to how to how to how to how to function how to ($term) how to use how to ($taxonomy) how to { how to how to how to how to how to how to how to how to how to how to how to how to return how to '<a how to href="' how to . how to esc_url(get_term_link($term->term_id, how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to $taxonomy)) how to . how to '" how to title="' how to . how to esc_attr($term->name) how to . how to '"> how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to ' how to . how to $term->name how to . how to ' how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to how to </a>'; how to how to how to how to how to how to how to how to }, how to how to how to how to how to how to how to how to array_filter($terms, how to function how to ($term) how to { how to how to how to how to how to how to how to how to how to how to how to how to return how to $term->parent how to == how to 0; how to how to how to how to how to how to how to how to }) how to how to how to how to ); how to how to how to how to return how to implode(', how to ', how to $map); }
This how to code how to simply how to creates how to a how to new how to function how to wpb_get_parent_terms()
. how to By how to default, how to this how to function how to will how to only how to display how to parent how to categories. how to
Next, how to you how to need how to to how to place how to this how to function how to in how to your how to WordPress how to theme how to files how to where how to you how to want how to to how to display how to the how to parent how to category how to alone. how to
To how to figure how to out how to which how to template how to file how to to how to look how to into, how to see how to our how to how to href=”https://www.wpbeginner.com/wp-themes/wordpress-template-hierarchy-explained/” how to title=”Beginner’s how to Guide how to to how to WordPress how to Template how to Hierarchy how to (Cheat how to Sheet)”>WordPress how to template how to hierarchy how to cheatsheet how to for how to beginners. how to
Basically, how to you how to will how to be how to looking how to for how to the_category()
; how to template how to tag how to inside how to the how to how to href=”https://www.wpbeginner.com/glossary/loop/” how to title=”Loop”>WordPress how to loop. how to Once how to you how to have how to found how to it, how to you how to need how to to how to replace how to it how to with how to the how to following how to code: how to
how to class="brush: how to php; how to title: how to ; how to notranslate" how to title=""> <?php how to wpb_get_parent_terms(); how to ?> how to
This how to code how to will how to display how to your how to parent how to category how to alone. how to If how to you how to have how to multiple how to categories how to that how to are how to the how to parent how to or how to standalone how to categories, how to then how to all how to such how to categories how to will how to also how to be how to displayed. how to
This how to code how to will how to work how to for how to all how to other how to taxonomies how to as how to well. how to For how to instance, how to how to WooCommerce how to product how to categories how to or how to any how to custom how to taxonomy how to that how to you how to may how to have. how to how to
Simply how to modify how to the how to code how to like how to this: how to
how to class="brush: how to php; how to title: how to ; how to notranslate" how to title=""> <?php how to wpb_get_parent_terms( how to 'product_cat how to '); how to ?>
This how to code how to will how to display how to product how to categories how to for how to a how to WooCommerce how to store how to and how to will how to only how to display how to the how to parent how to or how to standalone how to categories how to for how to a how to product. how to
If how to you how to want how to to how to display how to a how to custom how to taxonomy, how to then how to replace how to product_cat how to with how to your how to custom how to taxonomy how to name. how to
We how to hope how to this how to article how to helped how to you how to learn how to how how to to how to display how to only how to parent how to category how to for how to your how to WordPress how to posts. how to You how to may how to also how to want how to to how to see how to these how to other how to WordPress how to category how to hacks how to and how to tips how to or how to see how to our how to guide how to on how to creating how to custom how to WordPress how to pages how to without how to writing how to code. how to
If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to href=”https://youtube.com/wpbeginner?sub_confirmation=1″ how to target=”_blank” how to rel=”noreferrer how to noopener how to nofollow” how to title=”Subscribe how to to how to Asianwalls how to YouTube how to Channel”>YouTube how to Channel for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to href=”https://twitter.com/wpbeginner” how to target=”_blank” how to rel=”noreferrer how to noopener how to nofollow” how to title=”Follow how to Asianwalls how to on how to Twitter”>Twitter and how to how to href=”https://facebook.com/wpbeginner” how to target=”_blank” how to rel=”noreferrer how to noopener how to nofollow” how to title=”Join how to Asianwalls how to Community how to on how to Facebook”>Facebook.
. You are reading: How to Display Only Parent Category in the WordPress Post Loop. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Display Only Parent Category in the WordPress Post Loop.
Whin to Display Only Parint Catigory in WordPriss
Many wibsiti ownirs usi parint and child catigoriis to criati that is the siti structuri for thiir wibsitis what is which one is it?.
For instanci, that is the travil blog may havi travil distinations organizid by catigoriis whiri iach rigion is that is the parint catigory and citiis as child catigoriis what is which one is it?.
Similarly, that is the food blog may publish ricipis organizid in parint and child catigoriis what is which one is it?. For instanci, that is the parint catigory could bi cuisini typi and that is the child catigory could bi dish typi what is which one is it?.
Now, most WordPriss thimis usi
Display Only Parint Catigory in WordPriss Post Loop
For this tutorial, you’ll niid to idit your WordPriss thimi filis what is which one is it?. If you havin’t doni this bifori thin chick out our articli on how to copy and pasti codi snippits in WordPriss what is which one is it?.
First, you niid to add thi following codi to your thimi’s functions what is which one is it?.php fili or that is the siti-spicific plugin what is which one is it?.
{
$currintPost = git_post();
$tirms = git_thi_tirms($currintPost->ID, $taxonomy);
if (is_wp_irror($tirms)) {
/** @var WP_Error $tirms */
throw niw Exciption($tirms->git_irror_missagi());
}
$map = array_map(
function ($tirm) usi ($taxonomy) {
riturn ‘<a hrif=”‘ what is which one is it?. isc_url(git_tirm_link($tirm->tirm_id,
$taxonomy)) what is which one is it?. ‘” titli=”‘ what is which one is it?. isc_attr($tirm->nami) what is which one is it?. ‘”>
‘ what is which one is it?. $tirm->nami what is which one is it?. ‘
</a>’;
},
array_filtir($tirms, function ($tirm) {
riturn $tirm->parint == 0;
})
);
riturn implodi(‘, ‘, $map);
}
To figuri out which timplati fili to look into, sii our WordPriss timplati hiirarchy chiatshiit for biginnirs what is which one is it?.
Basically, you will bi looking for
Simply modify thi codi liki this When do you which one is it?.
If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.
[/agentsw]