Search Engine Optimized breadcrumbs

I had a need to build some search engine optimized breadcrumbs for a project I’m working on right now.  I didn’t want to just use Yoast’s breadcrumbs, because I’m using Twitter Bootstrap, and I wanted to make use of the built-in breadcrumbs support, so I did a Google, found something, tweaked it and am using it on this project.  I wanted it to support both page and blog post hierarchy, the original source did not support page hierarchy, just post categories.  Everything works automagically, all you need to do is use the seo_breadcrumbs() php function.

Here are some screenshots of it in action:


Breadcrumbs in a post, when a page has been set for blog posts (other than the Home page)


Breadcrumbs in a post, when no page has been set for blog posts (default Reading setting)


Breadcrumbs in a page, when the page has a parent


Breadcrumbs in a page, when there is no parent page

Here’s the code:

/**
* SEO Breadcrumbs
* @author Chris Reynolds
* @link http://www.quickonlinetips.com/archives/2012/02/wordpress-seo-breadcrumbs/
* Search engine optimized breadcrumbs. Original source was taken from the link above, with changes made so that it supports pages as well as posts and integrates into Twitter Bootstrap breadcrumb styles
*/
function seo_breadcrumbs() {
// this sets up some breadcrumbs for posts & pages that support Twitter Bootstrap styles
$separator = ' <span class="divider">&rsaquo;</span>';
echo '<ul xmlns:v="http://rdf.data-vocabulary.org/#" class="breadcrumb">';
global $post;
echo '<li><span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="' . get_home_url() . '">Home</a></span>' . $separator . '</li>';
if ( is_page() && $post->post_parent ) {
// get the parent page breadcrumb
$parent_title = get_the_title($post->post_parent);
if ( $parent_title != the_title(' ', ' ', false) ) {
echo '<li><span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href=' . get_permalink($post->post_parent) . ' ' . 'title=' . $parent_title . '>' . $parent_title . '</a></span>' . $separator . '</li>';
}
} else {
// first, display the blog page link, since that's a global parent, but only if it's set to be different than the home page
if ( get_option('page_for_posts') ) {
// defines the blog page if it's set
$blog_page_uri = get_permalink( get_option( 'page_for_posts' ) );
echo '<li><span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="' . $blog_page_uri . '">News</a></span>' . $separator . '</li>';
}
// this is a post, so get the category, if it exists
$category = get_the_category();
if ($category) {
foreach($category as $category) {
echo '<li><span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></span>' . $separator . '</li>';
}
}
}
echo '<li class="active">' . the_title() . '</li>';
echo '</ul>';
}
// syntax: <?php seo_breadcrumbs(); ?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.