Add Breadcrumb Navigation to WordPress with HTML and JSON-LD Structured Data

Breadcrumbs in WordPress: HTML and JSON-LD from one function

A breadcrumb trail shows where a page sits in the site: Home, category, sub-category, post. Visitors use it to go one level up, and Google shows it instead of the URL in search results when the page has BreadcrumbList structured data. Both should come from the same list, so the visible trail and the markup never disagree.

Problem: The theme has no breadcrumbs. Posts need Home, the category with its parents and the post; pages need their parent pages; archives need the term hierarchy.

Solution: Build the trail once in a function that returns an array of name/URL pairs, print it as an ordered list in a <nav aria-label="Breadcrumb">, and print the same array as BreadcrumbList JSON-LD in wp_head.

Build the trail

function ha_breadcrumb_items() {
    $items = [ [ __( 'Home', 'textdomain' ), home_url( '/' ) ] ];

    $add_terms = function ( $term ) use ( &$items ) {
        foreach ( array_reverse( get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' ) ) as $id ) {
            $parent  = get_term( $id, $term->taxonomy );
            $items[] = [ $parent->name, get_term_link( $parent ) ];
        }
        $items[] = [ $term->name, get_term_link( $term ) ];
    };

    if ( is_singular( 'post' ) ) {
        $categories = get_the_category();
        if ( $categories ) {
            $add_terms( $categories[0] );
        }
        $items[] = [ get_the_title(), get_permalink() ];
    } elseif ( is_page() ) {
        foreach ( array_reverse( get_post_ancestors( get_queried_object_id() ) ) as $id ) {
            $items[] = [ get_the_title( $id ), get_permalink( $id ) ];
        }
        $items[] = [ get_the_title(), get_permalink() ];
    } elseif ( is_category() || is_tag() || is_tax() ) {
        $add_terms( get_queried_object() );
    } elseif ( is_search() ) {
        $items[] = [ sprintf( __( 'Search results for "%s"', 'textdomain' ), get_search_query( false ) ), '' ];
    }

    return is_front_page() ? [] : $items;
}

Each item is [ name, url ]. The first category of a post is used; if posts have several categories, decide which one is primary (for example with a custom field) and use it here.

The visible trail

function ha_breadcrumbs() {
    $items = ha_breadcrumb_items();
    if ( ! $items ) {
        return;
    }
    echo '<nav class="ha-breadcrumbs" aria-label="' . esc_attr__( 'Breadcrumb', 'textdomain' ) . '"><ol>';
    $last = count( $items ) - 1;
    foreach ( $items as $i => [ $name, $url ] ) {
        if ( $i === $last || ! $url ) {
            echo '<li aria-current="page">' . esc_html( $name ) . '</li>';
        } else {
            echo '<li><a href="' . esc_url( $url ) . '">' . esc_html( $name ) . '</a></li>';
        }
    }
    echo '</ol></nav>';
}

Call ha_breadcrumbs() in the templates above the title. aria-current="page" marks the current page for screen readers.

.ha-breadcrumbs ol { display: flex; flex-wrap: wrap; gap: 0.25rem; list-style: none; margin: 0; padding: 0; }
.ha-breadcrumbs li + li::before { content: "/"; margin-right: 0.25rem; color: #888; }

The JSON-LD

add_action( 'wp_head', function () {
    $list = [];
    foreach ( ha_breadcrumb_items() as $i => [ $name, $url ] ) {
        $item = [ '@type' => 'ListItem', 'position' => $i + 1, 'name' => wp_strip_all_tags( $name ) ];
        if ( $url ) {
            $item['item'] = $url;
        }
        $list[] = $item;
    }
    if ( count( $list ) < 2 ) {
        return;
    }
    $data = [ '@context' => 'https://schema.org', '@type' => 'BreadcrumbList', 'itemListElement' => $list ];
    echo '<script type="application/ld+json">' . wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . "</script>\n";
} );

NOTE: If the theme already prints a BreadcrumbList for posts, as in JSON-LD structured data with PHP, remove that part and keep this one: two breadcrumb lists with different items on one page are a mistake Search Console reports.