If you are using a loop to list all your WordPress categories or terms in a custom taxonomy and you only want to display the top-level ones, here is how you do that. The trick is in getting the “parent” parameter, which is set to “0” for all top-level terms and categories. So to start a foreach loop and display all the terms that are parents, use this code:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
$taxonomy = 'custom_taxonomy_name'; //Choose the taxonomy $terms = get_terms( $taxonomy ); //Get all the terms foreach ($terms as $term) { //Cycle through terms, one at a time // Check and see if the term is a top-level parent. If so, display it. $parent = $term->parent; if ( $parent=='0' ) { //do something } |
Pretty simple, right? Nothing too complicated there. Now, what if you wanted to list all parent categories and link to their archive pages? We can do that! Here is the code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!--?php $taxonomy = 'custom_taxonomy_name'; //Choose the taxonomy $terms = get_terms( $taxonomy ); //Get all the terms foreach ($terms as $term) { //Cycle through terms, one at a time // Check and see if the term is a top-level parent. If so, display it. $parent = $term->parent; if ( $parent=='0' ) { $term_id = $term->term_id; //Define the term ID $term_link = get_term_link( $term, $taxonomy ); //Get the link to the archive page for that term $term_name = $term->name; echo '<a class="ccats" href="' . $term_link . '"><span class="label">' . $term_name . '</span></a>'; } } ?> |
There you have it! Easily link to all the top-level (parent) terms or categories in your custom taxonomy. Nothing too complicated here. Let me know if this helped you in the comments, or you can reach me on twitter @brianjonline.

16 Comments on “Display Only Top-Level Parent Categories – WordPress”
count;foreach ($terms as $term) {
echo ”;
echo ‘‘.$term->name.’‘;
echo ‘‘.$term->description.’‘;
echo ”.$term->count.”;
echo ”;
}
?>
This is my code. How can I modify it for showing only parent categories? Please help.
hi
how to display woocommerce parent categories in list style thanks
Thank you so much for this! I have been going crazy looking all over the internet for this exact thing!!
This code works PERFECTLY
Glad I could help out!
Hi Brian, Thanks very much for this example. I’m tearing my hair out with a situation very similar to you example above. I’m trying to output just the current category of the current page (not listing all of them) without the children. Your example works great, apart from I’ve tried to remove the cycling behaviour by foreach ($terms as $term) and just output the current one. I’m totally happy to admit I’m struggling with the php. I just feel I’m so close, yet so far!
Any help would be really appreciated.
Michael
If you shared the code we might be able to take a look!
Taking this a step further I’d like to prevent WP creating product archive pages for the non top level categories so Google doesn’t pick them up as I’m using the sub category as a filter on the parent category archive page.
I’d like a fully automated solution as the site owners are not technical to say the least. Any ideas?
Yoast SEO has a section where you can prevent taxonomies from being in the sitemap. I think you can also manually enter in pages (such as archives) to remove from the sitemap, so you might just want to manually remove non-top-level categories.
Don’t know how you could automate that though.
Line 8 on your second snippet of code has an error: $parent = $term—>parent;
should be: $parent = $term->parent;
Thank you for this – very helpful.
Right you are! I’m not sure what happened there, but nice catch! It’s been corrected.
Glad I could help out!