How to show breadcrumb-style category navigation? Get parent and child categories programmatically. Ready-made code.
EN

How to display current and parent category of a post (WordPress snippet)

5.00 /5 - (25 votes )
Last verified: May 1, 2026
2min read
Tutorial
Full-stack developer

When building custom WordPress themes, you often need to display not just the current category, but also its parent category (for breadcrumb navigation or hierarchical menus).

Learn more about professional WordPress development at WPPoland. WordPress stores categories in a hierarchical structure, but get_the_category() returns a flat array. Here’s how to extract the hierarchy.

#Method: Get_the_category() + parent check

Here’s a ready-made function you can paste into functions.php:

function wppoland_get_category_hierarchy() {
    $categories = get_the_category();
    
    if ( empty( $categories ) ) {
        return false;
    }
    
    // Get the first category
    $category = $categories[0];
    
    $output = '';
    
    // If category has a parent
    if ( $category->parent ) {
        $parent = get_category( $category->parent );
        $output .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . esc_html( $parent->name ) . '</a> &raquo; ';
    }
    
    // Current category
    $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>';
    
    return $output;
}

#Usage IN template

<?php
$hierarchy = wppoland_get_category_hierarchy();
if ( $hierarchy ) {
    echo '<div class="category-breadcrumb">' . $hierarchy . '</div>';
}
?>

This will output something like: Technology » WordPress

#Alternative: Full breadcrumb trail

If you need the complete hierarchy (grandparent, parent, child), use this recursive approach:

function wppoland_get_full_category_trail( $category_id ) {
    $trail = array();
    
    while ( $category_id ) {
        $category = get_category( $category_id );
        array_unshift( $trail, $category );
        $category_id = $category->parent;
    }
    
    $output = '';
    foreach ( $trail as $cat ) {
        $output .= '<a href="' . esc_url( get_category_link( $cat->term_id ) ) . '">' . esc_html( $cat->name ) . '</a> &raquo; ';
    }
    
    return rtrim( $output, ' &raquo; ' );
}

This solution is clean, efficient, and handles multi-level category hierarchies properly.

Next step

Turn the article into an actual implementation

This block strengthens internal linking and gives readers the most relevant next move instead of leaving them at a dead end.

Want this implemented on your site?

If you want to convert the article into a working site improvement, redesign, or build plan, I can define the scope and implement it.

Related cluster

Explore other WordPress services and knowledge base

Strengthen your business with professional technical support in key areas of the WordPress ecosystem.

Article FAQ

Frequently Asked Questions

Practical answers to apply the topic in real execution.

SEO-ready GEO-ready AEO-ready 3 Q&A
How long does it take to display parent and current category in WordPress?
About 10 minutes if you already know your way around theme template files. The snippet is short, but you usually drop it inside a single template such as single.php, archive.php, or a custom shortcode wrapper.
What is the minimum WordPress version required for this snippet?
Any modern WordPress release will do, since get_the_category(), get_category() and get_category_link() have been part of core for many years. PHP 7.4 or newer is recommended for compatibility with current WordPress requirements.
What can go wrong when displaying the category hierarchy?
The most common issue is assuming a post has only one category, while WordPress allows multiple. The other one is forgetting to escape output with esc_html() or esc_url(), which breaks markup or opens an XSS vector.

Need an FAQ tailored to your industry and market? We can build one aligned with your business goals.

Let’s discuss

Related Articles

Need to get the URL of the first image or link in an article? Use Regular Expressions (Regex) or DOMDocument. Code for developers.
development

How to extract the first link from post content (PHP snippet)

Need to get the URL of the first image or link in an article? Use Regular Expressions (Regex) or DOMDocument. Code for developers.

Stop writing messy if-statements. Learn the difference between in_category and has_term, how to handle recursive child categories efficiently, and optimize your conditional tags.
development

WordPress conditional logic for taxonomies

Stop writing messy if-statements. Learn the difference between in_category and has_term, how to handle recursive child categories efficiently, and optimize your conditional tags.

A practical WordPress snippet for adding parent and ancestor page slugs to body_class(), so you can target site sections with cleaner CSS.
wordpress

Add Parent Page Slugs to WordPress Body Classes

A practical WordPress snippet for adding parent and ancestor page slugs to body_class(), so you can target site sections with cleaner CSS.