WordPress Breadcrumbs for Pages

Breadcrumbs are very useful navigation tools, especially for sites with multiple levels and more than a dozen pages.  While there are a few breadcrumb navigation plugins for WordPress, sometimes it’s a bit of an overkill when the job can be done with just a few lines of code.

The function below will insert a breadcrumb navigation for the pages in your WP site, and highlight the news/blog page when within that section, etc.

Usage:

  1. Copy the code below into your theme’s functions.php file.  If your theme doesn’t have one, create it.
  2. //breadcrumb function
    function rm_bread_crumbs() {
     global $post;
     $crumbs =     '<p><a href="'.get_option('home').'">Home</a>';
    
     //if the page has a parent add title and link of parent
     if($post->post_parent) {
     $crumbs .=     ' &raquo; <a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>';
     }
    
     // if it's not the front page of the site, but isn't the blog either
     if((!is_front_page()) && (is_page())) {
     $crumbs .=    ' &raquo; '.get_the_title($post->ID);
     }
    
     //if it's the news/blog home page or any type of archive
     if((is_home() ||(is_archive()))) {
     $crumbs .=    ' &raquo; '.get_the_title(get_option(page_for_posts));
     }
    
     //if it's a single news/blog post
     if(is_single()) {
     $crumbs .=     ' &raquo; <a href="'.get_permalink(get_option(page_for_posts)).'">'.get_the_title(get_option(page_for_posts)).'</a>';
     $crumbs .=    ' &raquo; '.get_the_title($post->ID);
     }
     $crumbs .=    '</p>'."\n";
     echo $crumbs;
    }
  3. Insert <?php rm_bread_crumbs(); ?> in your theme where ever you’d like the bread crumbs displayed.