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 thoughts on “WordPress custom breadcrumbs without plugin”

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

  2. 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.

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

  4. 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. 🙂

  5. I got errors on WP 3.8.1 (“headers blah-blah” when I try to edit a post with activated breadrumbs from the template)

  6. 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!

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

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

    Thank you

  9. Great post! How to make this breadcrumbs function to noindex?, hide it from SERPs. Please help me out.

  10. 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”

  11. 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 . ‘ ‘;

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

Leave a Reply

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