Breadcrumb Navigation for WordPress

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"
1 trackback
  1. [...] 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 [...]

Leave a Response