A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

WordPress: How to create a breadcrumb without using any plugins

A quick post for WordPress users.

Go to your functions.php file. If it doesn’t exist, then create it.

wp-content/themes/theme_name/functions.php

Then add this inside the functions.php file.

function the_breadcrumb($post_ancestors) {
    if (!is_home()) {
        echo '<a href="';
        echo get_option('home');
        echo '">';
        echo 'Home';
        echo "</a> &gt; ";
        if (is_category() || is_single()) {
            the_category('title_li=');
            if (is_single()) {
                echo " &gt; ";
                the_title();
            }
         } elseif (is_page()) {
            foreach($post_ancestors AS $value) {
                echo "<a href='".get_permalink($value)."'>".get_the_title($value)."</a> &gt; ";
            }
            echo the_title();
        }
    }
}

Then in your wp-content/themes/theme_name/index.php or anywhere in your template files, you can do this:

<?php global $post; ?>
<div class="breadcrumbs"><?php the_breadcrumb(get_post_ancestors($post)); ?></div>

That should display something like this:
Home > A Sample Page > Sub for the Sample Page


* Most of the source code is from here Catswhocode.com. I edited it so that you can get the “parents” for the pages. Thanks to for the kick-start!

This entry was posted in General and tagged , . Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>