
According to the Wikipedia, breadcrumbs or breadcrumb trail are a navigation technique used in user interfaces. Its purpose is to give users a way to keep track of their location within programs or documents. I’ve been trying several codes and plugin but still unsatisfied. So I tried to create my own function and use it here and on my other blog. So, here’s my solution for the breadcrumb.
The Code
It using the WordPress conditional tag combine with other WordPress function to display the exact page title on the breadcrumb. Most of breadcrumb function I found only worked for page and post. In my function, I manage to make it able to display the link for category archive, sub-categories, date archive even for a search result page including post which get split by <!--nextpage--> and paged archives.
<?php
function wp_breadcrumb() {
global $cat,$s,$post,$wp_locale;
if ( get_the_category() ) $category = get_the_category();
if ( is_tag() ) $tag = get_term($tag_ID, 'post_tag', OBJECT, 'display');
if ( is_author() ) $userdata = get_userdata($author);
?><div id="breadcrumb"><ul><li class="start"><a href="<?php bloginfo('url'); ?>/" title="<?php bloginfo('name'); ?>"><?php echo __('Home'); ?></a></li><?php
if ( have_posts() ) :
// Display breadcrumb for category and sub-category archive
if ( is_category() ) {?><li><?php echo substr(get_category_parents($cat,true,'</li><li>'),0,-9); ?></li><?php }
// Display breadcrumb for calendar archive
elseif ( is_day() ) {?><li><a href="<?php echo get_year_link(get_the_time('Y')); ?>"><?php the_time('Y'); ?></a></li><li><a href="<?php echo get_month_link(get_the_time('Y'),get_the_time('m')); ?>"><?php the_time('F'); ?></a></li><li><a href="<?php echo get_day_link(get_the_time('Y'),get_the_time('m'),get_the_time('d')); ?>"><?php the_time('d'); ?></a></li><?php }
elseif ( is_month() ) {?><li><a href="<?php echo get_year_link(get_the_time('Y')); ?>"><?php the_time('Y'); ?></a></li><li><a href="<?php echo get_month_link(get_the_time('Y'),get_the_time('m')); ?>"><?php the_time('F'); ?></a></li><?php }
elseif ( is_year() ) {?><li><a href="<?php echo get_year_link(get_the_time('Y')); ?>"><?php the_time('Y'); ?></a></li><?php }
// Display breadcrumb for single post and attachments
elseif ( is_single() ) {?><li><?php echo substr(get_category_parents($category[0]->cat_ID,true,'</li><li>'),0,-9); ?></li><?php if($post->post_parent ) {?><li><a href="<?php echo get_permalink($post->post_parent); ?>"><?php echo get_the_title($post->post_parent); ?></a></li><?php } ?><li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li><?php }
// Display breadcrumb for pages
elseif ( is_page() ) {if ( $post->post_parent ) {?><li><a href="<?php echo get_permalink($post->post_parent); ?>"><?php echo get_the_title($post->post_parent); ?></a></li><?php } ?><li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li><?php }
// Display breadcrumb for search result page
elseif ( is_search() ) {?><li><a href="<?php echo clean_url( get_pagenum_link() ); ?>" title="<?php echo __('Search result for') . ' ' . wp_specialchars(get_query_var('s')); ?>"><?php echo __('Search result for') . ' ' . wp_specialchars(get_query_var('s')); ?></a></li><?php }
// Display breadcrumb for tag archive
elseif ( is_tag() ) {?><li><a href="<?php echo clean_url( get_pagenum_link() ); ?>" title="<?php echo __('Archive for tag') . ' ' . $tag->name; ?>"><?php echo __('Archive for tag') . ' ' . $tag->name; ?></a></li><?php }
// Display breadcrumb for author archive
elseif ( is_author() ) {?><li><a href="<?php echo clean_url( get_pagenum_link() ); ?>" title="<?php echo __('Article posted by') . ' ' . $userdata->display_name; ?>"><?php echo __('Article posted by') . ' ' . $userdata->display_name; ?></a></li><?php }
// Display breadcrumb for page which got split
if ( get_query_var('page') ) {?><li><a href="<?php get_permalink(); ?>" title="<?php echo __('Part'); ?> <?php echo get_query_var('page'); ?>"><?php echo __('Part'); ?> <?php echo get_query_var('page'); ?></a></li><?php }
// Display breadcrumb for paged archives
if ( get_query_var('paged') ) {?><li><a href="<?php get_permalink(); ?>" title="<?php echo __('Page'); ?> <?php echo get_query_var('paged'); ?>"><?php echo __('Page'); ?> <?php echo get_query_var('paged'); ?></a></li><?php }
endif;
?></ul>
</div>
<?php
}
?>
How to Use
Open and paste the above code into your functions.php file. Create one if you don’t have any. Now that we have it, you can simply add the code below into your header.php file.
<?php wp_breadcrumb(); ?>
Don’t forget to edit your stylesheet and add the style for the breadcrumb layer and list to match your theme:
#breadcrumb {}
#breadcrumb ul {}
#breadcrumb ul li {}
Now you have your breadcrumb. If you found any bugs or errors in this code, don’t hesitate to contact or leave a comment on this post regarding to the function.



9 comments on "Breadcrumb Navigation for WordPress"
I got an error. syntax error in functions.php on line blablabla (1042) :(
I’ve replace the displayed script after doing some check.
I couldn’t found any errors though.
Are you sure you paste the code correctly into the function.php?
I’m also getting an error in my functions.php. It’s causing conflict with header postings and not allowing me to login to the blog. When I replace functions.php with another, it works fine.
@Chris … can you explain what’s the error please? Because I can’t see what makes such an error, I’ve been using it on my other blog (http://preaxz.com) since forever. There, my functions.php is only consist of two functions, custom comment and this breadcrumb.
Have you used another breadcrumb function?
Theme Artist,clear car home run material demand surprise nation yeah doubt offer cheap collection information policy position book doubt trust use son free president charge legal clothes island milk threaten onto nor ball ancient criterion follow historical exchange succeed publication payment heat doctor begin largely sentence meeting fast always water feature hair lead lovely president farm generally battle notion husband communication career moment facility star spread odd town traditional her own technique its overall character below depend administration publication front owner pupil hope elderly advance to hole partner response trade elsewhere
function works perfect, but I would like want the current ‘page, post and category page’ not to be linked, how can I do that? I want to style the current page differently somehow.
Article Marketing is actually one of the best ways to promote a website and sell some products online.~–
Listen to this story with an open Heart.
Article marketing is really great if you have the time and effort to make new articles each day.*”;
1 trackback
[...] una ‘categoría padre’, que se mostrara también. Encontramos un ejemplo en el blog de Arie Putranto [en] que cumplía todo lo que necesitamos, os lo compartimos a [...]