Breadcrumbs with BreadcrumbList schema
Build a breadcrumb trail for pages, posts, archives and search, render it as an accessible list and add matching JSON-LD.
WordPressNavigationbreadcrumbsschema-orgseojson-ld
Code
How it works
What it solves
Breadcrumbs help visitors move up a level and can appear in search results when marked up properly. This helper produces the trail from the current query, an accessible list and the matching structured data from one source of truth.
How it works
The trail function checks what the current request is: a singular item (with its category or page ancestors), a term archive, a search or a 404. Each branch appends label and URL pairs. The renderer outputs a labelled nav with an ordered list, a link for each crumb except the last, which gets aria-current="page". The same array becomes a BreadcrumbList with positions, encoded with tag-safe JSON flags.
There is exactly one place to change the logic.
Adapting it
Add custom post types by inserting their archive link before the singular title. Use the primary category chosen by your SEO plugin instead of the first one. Style the separators with li + li::before. Hide the trail on the front page, as the count check already does.
Pitfalls
- Show only one breadcrumb schema per page; remove other plugins' output if both print.
- The
get_post_ancestorsorder is child first, so it is reversed. - Escape the label for HTML and strip tags for JSON.
- Translated labels must be the visible text.
How to use it
Save as inc/breadcrumbs.php and require it from functions.php.
Call acme_breadcrumbs(); in header.php or a template part below the header.
Style .acme-crumbs ol as an inline list.
Licence: GPL-2.0-or-later. This snippet comes from WordPress code and is GPL-2.0-or-later.