WordPress custom breadcrumbs without plugin

Breadcrumbs are the important part of our website, in wordpress there are several plugins are available for this, but below i posted the code for custom breadcrumbs for our wordpress site. Just add this below code in to your current wordpress theme functions.php and call the second step.

Step 1

[php]
‘; // tag before the current crumb
$after = ‘‘; // tag after the current crumb

global $post;
$homeLink = get_bloginfo(‘url’);

if (is_home() || is_front_page()) {

if ($showOnHome == 1) echo ‘

‘;

} else {

echo ‘

‘ . $home . ‘ ‘ . $delimiter . ‘ ‘;

if ( is_category() ) {
$thisCat = get_category(get_query_var(‘cat’), false);
if ($thisCat->parent != 0) echo get_category_parents($thisCat->parent, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
echo $before . ‘Archive by category “‘ . single_cat_title(”, false) . ‘”‘ . $after;

} elseif ( is_search() ) {
echo $before . ‘Search results for “‘ . get_search_query() . ‘”‘ . $after;

} elseif ( is_day() ) {
echo ‘‘ . get_the_time(‘Y’) . ‘ ‘ . $delimiter . ‘ ‘;
echo ‘‘ . get_the_time(‘F’) . ‘ ‘ . $delimiter . ‘ ‘;
echo $before . get_the_time(‘d’) . $after;

} elseif ( is_month() ) {
echo ‘‘ . get_the_time(‘Y’) . ‘ ‘ . $delimiter . ‘ ‘;
echo $before . get_the_time(‘F’) . $after;

} elseif ( is_year() ) {
echo $before . get_the_time(‘Y’) . $after;

} elseif ( is_single() && !is_attachment() ) {
if ( get_post_type() != ‘post’ ) {
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo ‘‘ . $post_type->labels->singular_name . ‘‘;
if ($showCurrent == 1) echo ‘ ‘ . $delimiter . ‘ ‘ . $before . get_the_title() . $after;
} else {
$cat = get_the_category(); $cat = $cat[0];
$cats = get_category_parents($cat, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
if ($showCurrent == 0) $cats = preg_replace(“#^(.+)\s$delimiter\s$#”, “$1”, $cats);
echo $cats;
if ($showCurrent == 1) echo $before . get_the_title() . $after;
}

} elseif ( !is_single() && !is_page() && get_post_type() != ‘post’ && !is_404() ) {
$post_type = get_post_type_object(get_post_type());
echo $before . $post_type->labels->singular_name . $after;

} elseif ( is_attachment() ) {
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
echo ‘‘ . $parent->post_title . ‘‘;
if ($showCurrent == 1) echo ‘ ‘ . $delimiter . ‘ ‘ . $before . get_the_title() . $after;

} elseif ( is_page() && !$post->post_parent ) {
if ($showCurrent == 1) echo $before . get_the_title() . $after;

} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = ‘display_name . $after;

} elseif ( is_404() ) {
echo $before . ‘Error 404’ . $after;
}

if ( get_query_var(‘paged’) ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ‘ (‘;
echo __(‘Page’) . ‘ ‘ . get_query_var(‘paged’);
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ‘)’;
}

echo ‘

‘;

}
} // end qt_custom_breadcrumbs()
?>
[/php]

Step 2

After adding the code into functions.php add the below code to where you want to use this function.
[php]

[/php]

35 replies
  1. gdisalvo
    gdisalvo says:

    thanks. I am using it. Is there a way to reverse it and stack the crumbs starting on the left and build out to the right? last-page << third-page << second-page << Home-page
    Incase I wanted to push this into a left hand corner as my right contains date and time stamps?

    Reply
  2. Michiel
    Michiel says:

    This is a good solution, but you could have added more elaborations on the code. Secondly, you might consider adding microdata for the breadcrumbs, so the breadcrumbs will also appear in google search results.

    Reply
  3. MicheleBG
    MicheleBG says:

    Very cool, it works with WP 3.6. I was wondering how to make it responsive. With max-width:767px do not display breadcrumbs. Via CSS it does not work.

    Reply
  4. Salih Kulangara
    Salih Kulangara says:

    Coool…Works Fine, but I want something more, I have custom post type and taxonomies, and how to work with this, In CPT single page I am getting home>Post Type>single post name, I want to show up the taxonomy term also… Like I have this, a Post type Publications, and taxonomy publication_type, and in publication_type terms like article, Poems etc, So I select a post from article, in single page i want to get Home>Publications>Poems>post title…. Is it possible???? Tanx in advance .

    Any How your work is cool, and so helpful. 🙂

    Reply
  5. Remus Adrian
    Remus Adrian says:

    Great job! But i have a question, my blog page is set on home.php, and with this breadcrumb it doesn’t show the blog title nor the blog title before a category. it’s like: Home >> Category >> Post, i want to show like: Home >> Blog >> Category >> Post… how can i do that? Thank you!

    Reply
  6. En Child
    En Child says:

    It’s very special, a few ways I use to find breadcrum on my site, just above methods can work. Thank the guest, I am very happy with the tutorial that you gave.

    Reply
  7. Guest
    Guest says:

    Superb… Your code helped us to add breakcumbs to our wordpress blog. Yeah…it just saved us from adding another plugin !!!

    Thank you

    Reply
  8. J Munce
    J Munce says:

    Thanks for the effort. How can I include the sections of the breadcrumb trail this misses?

    This code creates:

    Home » Category » Deeper Category » Current Post Title

    But I need:

    “The actual title of my site” » Category » Deeper Category » Deeper Category » Deeper Category » Deeper Category » “I don’t need current page title”

    Reply
  9. Ludo Briët
    Ludo Briët says:

    Delimiter doesn’t display correctly on Pages with multiple levels. To fix change line 81 from:

    if ($i != count($breadcrumbs)-1) echo ‘ ‘ . $delimiter . ‘ ‘;

    to:

    if ($i != count($breadcrumbs)-1) $returnString .= ‘ ‘ . $delimiter . ‘ ‘;

    Reply
  10. Adi Perdana
    Adi Perdana says:

    I got this error : Strict Standards: Only variables should be passed by reference in C:xampphtdocswp-theme-devwp-contentthemesviperincviper-functions.php on line 231, what should i do?

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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