Need a Wordpress breadcrumb navigation trail for your Wordpress theme, but do not want to rely on a plugin?
If you want your Wordpress theme to stand out from the crowd it is worth considering implementing a breadcrumb navigation trail. A breadcrumb trail can make it easier for your site visitors to identify what page they are currently viewing.
Plugins like Breadcrumb NavXT are available that will automatically produce a breadcrumb trail for you. However, if you are looking for an easier method that does not require a plugin the below solution should help.
Breadcrumb.php code
Copy the below code into a new file called breadcrumb.php – make sure this file is in your root theme folder.
<div id="breadcrumb">
<?php /* If this is the homepage */ if (is_home()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a></p>
<?php } ?>
<?php /* If this is a tag archive */ if (is_tag() ) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> » <a href="#" rel="bookmark" title="Posts Tagged ‘<?php single_tag_title(); ?>’">Posts Tagged ‘
<?php single_tag_title(); ?>
’</a></p>
<?php } ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> » <a href="#" title="<?php single_cat_title(); ?>">
<?php single_cat_title(); ?>
</a> </p>
<?php } ?>
<?php if /* If this is a page */ (is_page()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> »
<?php
global $wp_query;
if (empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
echo '';
} else {
$parent = $wp_query->post->post_parent;
echo '<a href="'.get_permalink($parent).'">'.get_the_title($parent).'</a> »';
}
?>
<a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
<?php the_title(); ?>
</a></p>
<?php } ?>
<?php if /* If this is a blog post */ (is_single()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> »
<?php the_category(', ') ?>
» <a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
<?php the_title(); ?>
</a></p>
<?php } ?>
<?php if /* If this is a search page */ (is_search()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> » <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a></p>
<?php } ?>
<?php /* If this is a 404 page */ if (is_404()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> » <a href="#" title="404 Error page">404 Error</a></p>
<?php } ?>
<?php /* If this is a yearly archive */ if (is_year()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> » <a href="<?php bloginfo('siteurl'); ?>/archives" title="archives"><span>archives</span></a> » <a href="#" title="Year">
<?php the_time('Y'); ?>
</a></p>
<?php } ?>
<?php /* If this is a monthly archive */ if (is_month()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> » <a href="<?php bloginfo('siteurl'); ?>/archives" title="archives"><span>archives</span></a> » <a href="#" title="Month">
<?php the_time('F, Y'); ?>
</a></p>
<?php } ?>
<?php /* If this is a daily archive */ if (is_day()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> » <a href="<?php bloginfo('siteurl'); ?>/archives" title="archives"><span>archives</span></a> » <a href="#" title="Month">
<?php the_time('F jS, Y'); ?>
</a></p>
<?php } ?>
<?php /* If this is a author archive */ if (is_author()) { ?>
<p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> » <a href="<?php bloginfo('siteurl'); ?>/archives" title="archives"><span>authors</span></a> »
<?php the_author_posts_link(); ?>
<?php
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;
?>
<a title="<?php echo $curauth->display_name; ?>'s posts" href="#"><?php echo $curauth->display_name; ?></a> </p>
<?php } ?>
</div>
<!-- end breadcrumb -->
Call the above template anywhere in your theme files (usually in header.php) using the following code:
<?php include (TEMPLATEPATH . "/breadcrumb.php"); ?>
The end HTML result will look something like this:
<div id="breadcrumb"> <p> <strong>You are here:</strong> <a href="http://yoursitename.com" title="home"><span>home</span></a></p> </div><!-- end breadcrumb -->
Known issues!
This does not work for Grandchild categories. In my experience, grandchild categories are used very rarely. If you have found a workaround or any improvements to this code let me know.

Will
Today
Be the first to leave a comment!