Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To show only top-level categories assigned to the current post, get that post’s categories with get_the_category() and keep terms whose parent is 0. Put the code below inside the loop where you want the category links to appear. If you mean the root ancestor of a deeply nested assigned category, use the ancestor-walking version instead.

Show assigned top-level categories

get_the_category() returns the categories assigned to the current post in WordPress’s built-in category taxonomy. A category with a parent value of 0 is top-level. Filtering the returned terms therefore shows only top-level categories that are actually assigned to this post—not every top-level category on the site. See the WordPress reference for get_the_category() and the term reference example using parent.

<?php
$categories = get_the_category();
$parent_categories = array_filter(
    $categories,
    static function ( $category ) {
        return 0 === (int) $category->parent;
    }
);

if ( ! empty( $parent_categories ) ) {
    $links = array();
    foreach ( $parent_categories as $category ) {
        $links[] = sprintf(
            '<a href="%s">%s</a>',
            esc_url( get_category_link( $category->term_id ) ),
            esc_html( $category->name )
        );
    }
    echo implode( ', ', $links );
}
?>

The URL is escaped with esc_url(), and the category name with esc_html(). If no assigned top-level categories remain after filtering, this snippet prints nothing.

Choose what “parent category” means for nested terms

The filter above checks each assigned category’s immediate parent. It does not automatically replace an assigned child or grandchild with its root ancestor. Choose the behavior that matches your post data:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Only assigned top-level categories: use the filter above. Assigned child categories are omitted.
  • Root categories for assigned descendants: start with the post’s assigned categories, follow each term’s parent chain until reaching a term whose parent is 0, then deduplicate the resulting roots before making links. A grandchild’s immediate parent is still a child, so checking only whether its parent is zero will not find the root.
  • Full hierarchy path: use get_the_category_list() with its parents option when you want parent-path text, rather than only the root name. WordPress documents the option values as multiple, single, or empty; see the function reference.

Why not query all top-level categories?

get_categories( array( 'parent' => 0 ) ) retrieves top-level categories from the taxonomy. It is appropriate for a general category list, but it does not by itself restrict results to categories assigned to the post currently being displayed. For a per-post display, filter the current post’s terms as shown above. See the get_categories() reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use the right function for the template context

Outside the loop

By default, get_the_category() uses the current loop post. Outside the loop, pass the post ID: get_the_category( $post_id ). WordPress documents this use in its function reference.

Rank #2
Sale
1,000 Books to Read Before You Die: A Life-Changing List
  • Book - 1, 000 books to read before you die: a life-changing list (1000 before you die)
  • Language: english
  • Binding: hardcover

For a custom taxonomy

get_the_category() is for the built-in category taxonomy. For a custom taxonomy, retrieve the post’s terms with get_the_terms( $post_id, 'taxonomy_name' ), handle an empty or error return, and apply the same parent or ancestor logic to those terms. The category function documentation points custom taxonomy use to get_the_terms().

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.