How to Easily Add WordPress Breadcrumb Trail

Handy code to add Wordpress breadcrumb trail in your templates to output a breadcrumb trail and navigation links on your blog.

By Tim TrottWordPress • January 24, 2009

I did not write this WordPress breadcrumb trail function, however, it's one of the ones in my global functions.php which is used on most of my sites. I cannot remember where I got it from or who wrote it.

php
function breadcrumbTrail($crumbs = true, $title = 'Browse', $separator = '/') 
{
  global $post;
?>
  <div class="breadcrumbs">
<?php
  if($title !== 'Browse') 
    echo $title;
  else 
    _e('Browse','options');
    
?>: <a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>"><?php _e('Home','options'); ?></a> <?php echo $separator; ?>
<?php
  
  if(is_single()) :
    the_category(', '); echo ' ' . $separator . ' ';
  elseif(is_page()) :
    $parent_id = $post->post_parent;
    $parents = array();
    while($parent_id) :
      $page = get_page($parent_id);
      if($params["link_none"]) 
        $parents[] = get_the_title($page->ID);
      else 
        $parents[]  = '<a href="'.get_permalink($page->ID).'" title="'.get_the_title($page->ID).'">'.get_the_title($page->ID).'</a> ' . $separator . ' ';
      $parent_id  = $page->post_parent;
    endwhile;
  
    $parents = array_reverse($parents);
    foreach($parents as $val) :
      echo $val;
    endforeach;
  endif;
  the_title(); 
?>
  </div>
<?php
}

To use this function simply make a call to breadcrumbTrail() in the template where you wish to show the WordPress Breadcrumb Trail.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 2 comments. Why not join the discussion!

New comments for this post are currently closed.