Display Only Top-Level Parent Categories – WordPress

Old People, Probably Grandparents

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:

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:

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.

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

16 Comments on “Display Only Top-Level Parent Categories – WordPress”

  1. 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.

  2. Thank you so much for this! I have been going crazy looking all over the internet for this exact thing!!
    This code works PERFECTLY

  3. 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

  4. 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?

    1. 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.

  5. 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.

    1. 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/

      1. 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 ‘|’.

        1. 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 😉

          1. 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;
            }
            ?>

  6. Pingback: Dropdown Menu of All Terms in Custom Taxonomy - Wordpress | Brian Johnson's Design Blog

Leave a Reply

Your email address will not be published. Required fields are marked *