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>";
   };
}

6 Responses to WordPress: better breadcrumb function

  1. is this compatible with wp 3.0+ ?

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

  3. 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?

  4. Never mind. It is working for me. Thanks!

Trackbacks/Pingbacks

  1. wp-popular.com » Blog Archive » Be Studios – blog › Wordpress: better breadcrumb function
  2. Algunos links para desarrolladores de WP | AplicacionesWeb

Leave a Reply