Semantic Breadcrumbs¶
Since v2.8.7 a basic template tag (amt_breadcrumbs()
) for the generation of a semantic breadcrumb trail (Schema.org enhanced) for hierarchically structured content types, such as pages, is available for use in your themes.
Below is an example about how to use the template tag in your theme templates to generate a list of Schema.org microdata enabled breadcrumbs for WordPress Pages:
<?php
if ( is_page() && function_exists('amt_breadcrumbs') ) {
amt_breadcrumbs( array(
// ID of list element.
'list_id' => 'breadcrumbs',
// Show breadcrumb item for the home page.
'show_home' => true,
// Text for the home link (requires show_home=true).
'home_link_text' => 'Home',
// Show breadcrumb item for the last page.
'show_last' => true,
// Show last breadcrumb as link (requires show_last=true).
'show_last_as_link' => true,
// Separator. Set to empty string for no separator.
'separator' => '>'
)); }
?>
The amt_breadcrumbs()
template tag generates a HTML unordered list. By default, the ID attribute of the list is breadcrumbs
or whatever you have set in the list_id
option of the template tag (see above).
The following is entirely optional and not required in order to have semantic breadrumbs.
In order to connect the breadcrumbs
Schema.org object to the web page's main Schema.org object which represents your content, the following manual actions are necessary:
1. By default, the main Schema.org object that is autogenerated by Add-Meta-Tags for your content is the Article. Since the Article
object does not support a breadcrumb property, it is essential to replace it with the WebPage object, which supports a hierarchical structure. This can easily be done by adding the following code to your theme's functions.php
:
function amt_schemaorg_set_webpage_entity_on_pages( $default ) {
if ( is_page() ) {
return 'WebPage';
}
return $default; // Article
}
add_filter( 'amt_schemaorg_object_main', 'amt_schemaorg_set_webpage_entity_on_pages' );
2. Now that the main Schema.org object has been set to WebPage, we also need to interconnect it with the BreadcrumbList object, which is generated by the amt_breadcrumbs()
template tag. This can be done by adding the breadcrumbs
object's ID in the itemref
attribute of the WebPage object with the following code (again in the functions.php
file of the theme):
function amt_set_itemref() {
if ( is_page() ) {
// Should return space delimited list of entity IDs
return 'breadcrumbs';
}
return '';
}
add_filter( 'amt_schemaorg_itemref_content', 'amt_set_itemref' );
Now your semantic breadcrumbs have been connected to the main Schema.org object (WebPage).
In case you use code that generates other Schema.org objects throughout the web page, you can connect those objects to the main Schema.org object by attaching a filtering function like amt_set_itemref
to the amt_schemaorg_itemref_content
hook. The function must return a space delimited list of IDs.