How to Create Schemas Breadcrumbs for Your WordPress Site

schema-breadcrumbs-in-wordpressSchema and Search Engine come together to bring a richer web data for your website back in 2011. The transition from displaying a simple site title, site url and site description is now fully evolve into a more complex search result where you can display video and news thumbnail, user reviews and ratings and many more schema supported microdata for search engines.

You can learn how to add schemas microdata to your WordPress site with this tutorial. If you want a hussle free method, you can installed this plugin to add schemas microdata to your WordPress blog.

But… what about adding schemas breadcrumbs to your WordPress site?

Don’t worry, you do not need to be a SEO or Schemas expert to add an effective schemas breadcrumbs for your WordPress site. Just open your theme functions.php and add this code.

[php]
function dez_schema_breadcrumb() {
global $post;
//schema link
$schema_link = 'http://data-vocabulary.org/Breadcrumb';
$home = 'Home';
$delimiter = ' » ';
$homeLink = get_bloginfo('url');
if (is_home() || is_front_page()) {
// no need for breadcrumbs in homepage
}
else {
echo '<div id="mpbreadcrumbs">';
// main breadcrumbs lead to homepage
if (!is_single()) {
echo 'You are here: ';
}
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . $homeLink . '">' . '<span itemprop="title">' . $home . '</span>' . '</a></span>' . $delimiter . ' ';
// if blog page exists
if (get_page_by_path('blog')) {
if (!is_page('blog')) {
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_permalink(get_page_by_path('blog')) . '">' . '<span itemprop="title">Blog</span></a></span>' . $delimiter . ' ';
}
}
if (is_category()) {
$thisCat = get_category(get_query_var('cat'), false);
if ($thisCat->parent != 0) {
$category_link = get_category_link($thisCat->parent);
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . $category_link . '">' . '<span itemprop="title">' . get_cat_name($thisCat->parent) . '</span>' . '</a></span>' . $delimiter . ' ';
}
$category_id = get_cat_ID(single_cat_title('', false));
$category_link = get_category_link($category_id);
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . $category_link . '">' . '<span itemprop="title">' . single_cat_title('', false) . '</span>' . '</a></span>';
}
elseif (is_single() && !is_attachment()) {
if (get_post_type() != 'post') {
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . $homeLink . '/' . $slug['slug'] . '">' . '<span itemprop="title">' . $post_type->labels->singular_name . '</span>' . '</a></span>';
echo ' ' . $delimiter . ' ' . get_the_title();
}
else {
$category = get_the_category();
if ($category) {
foreach ($category as $cat) {
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_category_link($cat->term_id) . '">' . '<span itemprop="title">' . $cat->name . '</span>' . '</a></span>' . $delimiter . ' ';
}
}
echo get_the_title();
}
}
elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
$post_type = get_post_type_object(get_post_type());
echo $post_type->labels->singular_name;
}
elseif (is_attachment()) {
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID);
$cat = $cat[0];
echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_permalink($parent) . '">' . '<span itemprop="title">' . $parent->post_title . '</span>' . '</a></span>';
echo ' ' . $delimiter . ' ' . get_the_title();
}
elseif (is_page() && !$post->post_parent) {
$get_post_slug = $post->post_name;
$post_slug = str_replace('-', ' ', $get_post_slug);
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_permalink() . '">' . '<span itemprop="title">' . ucfirst($post_slug) . '</span>' . '</a></span>';
}
elseif (is_page() && $post->post_parent) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_permalink($page->ID) . '">' . '<span itemprop="title">' . get_the_title($page->ID) . '</span>' . '</a></span>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
for ($i = 0; $i < count($breadcrumbs); $i++) {
echo $breadcrumbs[$i];
if ($i != count($breadcrumbs) - 1)
echo ' ' . $delimiter . ' ';
}
echo $delimiter . '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_permalink() . '">' . '<span itemprop="title">' . the_title_attribute('echo=0') . '</span>' . '</a></span>';
}
elseif (is_tag()) {
$tag_id = get_term_by('name', single_cat_title('', false), 'post_tag');
if ($tag_id) {
$tag_link = get_tag_link($tag_id->term_id);
}
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . $tag_link . '">' . '<span itemprop="title">' . single_cat_title('', false) . '</span>' . '</a></span>';
}
elseif (is_author()) {
global $author;
$userdata = get_userdata($author);
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_author_posts_url($userdata->ID) . '">' . '<span itemprop="title">' . $userdata->display_name . '</span>' . '</a></span>';
}
elseif (is_404()) {
echo 'Error 404';
}
elseif (is_search()) {
echo 'Search results for "' . get_search_query() . '"';
}
elseif (is_day()) {
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_year_link(get_the_time('Y')) . '">' . '<span itemprop="title">' . get_the_time('Y') . '</span>' . '</a></span>' . $delimiter . ' ';
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . '<span itemprop="title">' . get_the_time('F') . '</span>' . '</a></span>' . $delimiter . ' ';
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_day_link(get_the_time('Y'), get_the_time('m'), get_the_time('d')) . '">' . '<span itemprop="title">' . get_the_time('d') . '</span>' . '</a></span>';
}
elseif (is_month()) {
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_year_link(get_the_time('Y')) . '">' . '<span itemprop="title">' . get_the_time('Y') . '</span>' . '</a></span>' . $delimiter . ' ';
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . '<span itemprop="title">' . get_the_time('F') . '</span>' . '</a></span>';
}
elseif (is_year()) {
echo '<span itemscope itemtype="' . $schema_link . '"><a itemprop="url" href="' . get_year_link(get_the_time('Y')) . '">' . '<span itemprop="title">' . get_the_time('Y') . '</span>' . '</a></span>';
}
if (get_query_var('paged')) {
if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
echo ' (';
echo __('Page') . ' ' . get_query_var('paged');
if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
echo ')';
}
echo '</div>';
}
}
[/php]

or you can download the source code here. and add this php function code anywhere in your theme/template file.
[php]<?php echo dez_schema_breadcrumb(); ?>[/php]

After adding the code, you can use Google Structured Data Testing Tool to test your newly added schema breadcrumb data.

Final result in search data will be like this:
dezzain-schema-breadcrumb-result

Can This Schema benefit your site SEO?

Personally i’ve just added the Schema Breadcrumbs to my site last week so i can’t be sure if it will benefit your site SEO or traffics. However given that Google are keen to preferred a more structuralist data website, in time, your site’s search engine ranking might get a surprise boost in future.

Scroll to Top