Skip to content

Instantly share code, notes, and snippets.

@jonesch
Created September 10, 2012 16:48
Show Gist options
  • Save jonesch/3692059 to your computer and use it in GitHub Desktop.
Save jonesch/3692059 to your computer and use it in GitHub Desktop.
Wordpress Breadcrumb Trail - Pages & Post
<?php // Let's make a trail of crumbs, whether we are on the post or the page....This DOES NOT support custom Post Types...
//Create an is_blog function
function is_blog($post) {
$posttype = get_post_type($post);
return ( ((is_archive()) || (is_author()) || (is_category()) || (is_home()) || (is_single()) || (is_tag())) && ( $posttype == 'post') ) ? true : false ;
}
$bread_crumb_trail = '<span><a href="'.get_bloginfo('url').'">Home</a></span> &raquo;';
//Ancestors will only live on hierarchical posts...just pages.
if(!empty($post->ancestors)){
foreach($post->ancestors as $parent_id){
$bread_crumbs[$parent_id] = get_the_title($parent_id);
}
$bread_crumbs = array($post->ID => get_the_title()) + $bread_crumbs;
$bread_crumbs = array_reverse($bread_crumbs, 1);
foreach($bread_crumbs as $id => $title) {
if($id == $post->ID){
$bread_crumb_trail .= '<span>'.$title.'</span>';
} else {
$bread_crumb_trail .= '<span><a href="'.get_permalink($id).'">'.$title.'</a></span> &raquo; ';
}
}
//Now let's deal with the 'post' postype....There is a new function called is_blog that we created here.
} elseif(is_blog($post)) {
$blog_url_slug = 'blog/'; //Do you have anything that preceeds all of your blog posts...
$blog_homepage_url = get_bloginfo('url').'/'.$blog_url_slug;
if(is_home()){
$bread_crumb_trail .= '<span>Blog Archive</span>';
} elseif(is_category()){
$bread_crumb_trail .= '<span><a href="'.$blog_homepage_url.'">Blog Archive</a></span> &raquo; ';
$bread_crumb_trail .= '<span>'.single_cat_title('', false).'</span>';
} else {
$category_archive_link = rtrim(str_replace($post->post_name, '', get_permalink($post->ID)), '/');
$category_archive_title = str_replace('-', ' ', $category_archive_link);
$category_archive_title = str_replace($blog_homepage_url, ' ', $category_archive_link);
$bread_crumb_trail .= '<span><a href="'.$blog_homepage_url.'">Blog Archive</a></span> &raquo; ';
$bread_crumb_trail .= '<span><a href="'.$category_archive_link.'">'.ucwords($category_archive_title).'</a></span> &raquo; ';
$bread_crumb_trail .= '<span>'.get_the_title().'</span>';
}
} else {
$bread_crumb_trail .= '<span>'.get_the_title().'</span>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment