DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

If you want to add a breadcrumb to your blog, you can use one of the many available plugins to do so. But why using a plugin when you can easily code it yourself? Here’s a ready to use code snippet to add a breadcrumb to your WordPress blog.

Open your functions.php and paste the code below in it:

function the_breadcrumb() {
                echo '<ul id="crumbs">';
        if (!is_home()) {
                echo '<li><a href="';
                echo get_option('home');
                echo '">';
                echo 'Home';
                echo "</a></li>";
                if (is_category() || is_single()) {
                        echo '<li>';
                        the_category(' </li><li> ');
                        if (is_single()) {
                                echo "</li><li>";
                                the_title();
                                echo '</li>';
                        }
                } elseif (is_page()) {
                        echo '<li>';
                        echo the_title();
                        echo '</li>';
                }
        }
        elseif (is_tag()) {single_tag_title();}
        elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
        elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
        elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
        elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
        elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
        elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
        echo '</ul>';
}

Once done, open header.php and call the function:

<?php the_breadcrumb(); ?>

Thanks to emoticode for this snippet!

One Comment

  1. Nice to see it evolve. I made something similar based on your original post:

    leejacksondev.com/adding-breadcrumbs-to-your-wordpress-theme-no-plugin-required/

    Sometimes though a plugin is best if the client wants to change the settings and you want to avoid fiddling with code.

    🙂

Leave a Comment

Your email address will not be published. Required fields are marked *