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!
Very clever. Simple question (I hope) though. How do we add a separator for the terms?
The line: [php]echo ‘<a class="ccats" href="’ . $term_link . ‘"><span class="label">’ . $term_name . ‘</span></a>’; [/php] is where the actual html comes from. If you wanted to add something between them, you would just modify that to add something. For instance, you could change it to this:
[php]echo ‘<a class="ccats" href="’ . $term_link . ‘"><span class="label">’ . $term_name . ‘</span></a> | ‘; [/php]
To add a | between each link.
IF you wanted to get really fancy, you could combine it with my code for creating a dropdown menu here: https://pagecrafter.com/dropdown-menu-of-all-terms-in-custom-taxonomy-wordpress/
Thanks for the reply. Yeah had the same idea as you suggested. But was actually hoping to add the ‘separator’ argument somewhere as in ID, ‘custom_cat’, ”, ‘, ‘, ”); ?> so that when there’s only one term it doesn’t end with a superfluous ‘|’.
Wooops. Just noticed I was using your code completely wrong. Was using it to display only the parent terms of a custom post type.. Didn’t work as it was displaying all terms. It did display only the parents though 😉
This was the snippet I was looking for:
ID, $taxonomy, array( ‘fields’ => ‘ids’ ) );
// separator between links
$separator = ‘| ‘;
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ‘,’ , $post_terms );
$terms = wp_list_categories( ‘depth=1&title_li=&style=none&echo=0&taxonomy=’ . $taxonomy . ‘&include=’ . $term_ids );
$terms = rtrim( trim( str_replace( ”, $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
Pingback: Dropdown Menu of All Terms in Custom Taxonomy - Wordpress | Brian Johnson's Design Blog