Be Studios

Intercontinental Design Firm

This is our knowledge center. Enjoy!

WordPress: better breadcrumb function

This function is used to construct breadcrumbs of pages, posts and archives within Wordpress. Put this on functions.php inside yout theme and call anywhere you need (single.php, page.php) using:

<?php the_breadcrumb(); ?>

function the_breadcrumb() {
   $current = $post->ID;
   $parent = $post->post_parent;
   $grandparent_get = get_post($parent);
   $grandparent = $grandparent_get->post_parent;
   if (!is_home()) {
      echo '<li><a href="';
      echo get_option('home');
      echo '">';
      echo 'Home';
      echo "</a> »</li> ";
      if (is_category() || is_single() || is_archive()) {
         the_category('title_li=');
         if (is_single()) {
            echo " <li>» ";
            the_title();
            echo " </li>";
         }
      } elseif (is_page()) {
         if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {
            echo "<li><a href='";
            echo get_permalink($grandparent_get->post_parent);
            echo "'>";
            echo get_the_title($grandparent);
            echo "</a>";
            echo " » </li>";
         }
         echo '<li>';
         echo the_title();
         echo '</li>';
      }
   } else if ( is_home() ) {
      echo '<li><a href="';
      echo get_option('home');
      echo '">';
      echo 'Home';
      echo "</a></li>";
   };
}

Comments

Adrian: is this compatible with wp 3.0+ ?

jorge: Not tested, but it should work as the code isn't related to any particular function, but the ones in the core of WP.

Michael: Thanks for this simple function. Seems to be working for me except for the_title(). I'm calling in page.php in 3.x. Any idea why the title wouldn't be getting printed out?

Michael: Never mind. It is working for me. Thanks!