Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress: Breadcrumbs function
<?php
/*=============================================
= BREADCRUMBS =
=============================================*/
// to include in functions.php
function the_breadcrumb() {
$sep = ' > ';
if (!is_front_page()) {
// Start the breadcrumb with a link to your homepage
echo '<div class="breadcrumbs">';
echo '<a href="';
echo get_option('home');
echo '">';
bloginfo('name');
echo '</a>' . $sep;
// Check if the current page is a category, an archive or a single page. If so show the category or archive name.
if (is_category() || is_single() ){
the_category('title_li=');
} elseif (is_archive() || is_single()){
if ( is_day() ) {
printf( __( '%s', 'text_domain' ), get_the_date() );
} elseif ( is_month() ) {
printf( __( '%s', 'text_domain' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
} elseif ( is_year() ) {
printf( __( '%s', 'text_domain' ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
} else {
_e( 'Blog Archives', 'text_domain' );
}
}
// If the current page is a single post, show its title with the separator
if (is_single()) {
echo $sep;
the_title();
}
// If the current page is a static page, show its title.
if (is_page()) {
echo the_title();
}
// if you have a static page assigned to be you posts list page. It will find the title of the static page and display it. i.e Home >> Blog
if (is_home()){
global $post;
$page_for_posts_id = get_option('page_for_posts');
if ( $page_for_posts_id ) {
$post = get_post($page_for_posts_id);
setup_postdata($post);
the_title();
rewind_posts();
}
}
echo '</div>';
}
}
/*
* Credit: http://www.thatweblook.co.uk/blog/tutorials/tutorial-wordpress-breadcrumb-function/
*/
?>
<!-- start breadcrumbs -->
<?php the_breadcrumb(); ?>
<!-- end breadcrumbs -->
@ddcddc

This comment has been minimized.

Copy link

@ddcddc ddcddc commented Aug 23, 2017

Thanks!!

@StuCowley

This comment has been minimized.

Copy link

@StuCowley StuCowley commented May 15, 2018

Thank you so much @tinotriste this saved me so much time, easy to implement, well documented and easy to customise! Keep up the good work mate 😄

@29lim85

This comment has been minimized.

Copy link

@29lim85 29lim85 commented Jul 7, 2018

Time saver ! ! !

@carlosjulian

This comment has been minimized.

Copy link

@carlosjulian carlosjulian commented Jul 13, 2018

Thanks!!! easy of implement to my web.

@shihning

This comment has been minimized.

Copy link

@shihning shihning commented Aug 21, 2018

Thank You!!

@wh4t3ver92

This comment has been minimized.

Copy link

@wh4t3ver92 wh4t3ver92 commented Sep 17, 2018

Thx you for this code.
Can I use this code in latest version of my WordPress skin published here: https://wordpress.org/themes/browse/new/ and if yes under what licension? :)

@MediaSmackShahzad

This comment has been minimized.

Copy link

@MediaSmackShahzad MediaSmackShahzad commented Apr 16, 2019

Hi
I use this code it's working well, but when I select two categories things going wrong. The title_li= between two categories.
Wordpress > Testtitle_li=Uncategorized > test post 2

@ahmadthedev

This comment has been minimized.

@MediaSmackShahzad

This comment has been minimized.

Copy link

@MediaSmackShahzad MediaSmackShahzad commented Apr 16, 2019

Thank you @muhammadahmed53766. It's working
Wordpress » Test / Uncategorized » test post 2

@boutmos

This comment has been minimized.

Copy link

@boutmos boutmos commented Sep 9, 2020

Thanx you easy to understand, easy to customize : So I replace mine by your one ! ;)

@mikemike

This comment has been minimized.

Copy link

@mikemike mikemike commented Oct 25, 2020

There's some quite big parts missing on this, namely:

  • If you have any custom post types then you get 2 separators and no link to the parent page shown
  • No support for any kind of parent pages (get_post_ancestors())
  • No accessibility (aria) labels
  • It's using deprecating functions like get_page()

I've re-written it to fix all of the above problems but it's now quite specific to my project as I've had to hard-code in the parent pages for certain CPT. Happy to share if anyone would find it useful though. I've also set it to return values rather than outputting directly.

@boutmos

This comment has been minimized.

Copy link

@boutmos boutmos commented Nov 10, 2020

@mikemike : Do you have a link to look at your improvements ? thanx ;)

@joshbali

This comment has been minimized.

Copy link

@joshbali joshbali commented Nov 22, 2020

Nice basic and easy to understand breadcrumbs thing which helps me to get into the subject.

Edit: Forked and changed to show static blog link on categories/single posts and to display multiple categories (if applicable.

Thanks for the code even if it does not suit all possible situations. But great as a starter for a newbie like me...

@Fradd747

This comment has been minimized.

Copy link

@Fradd747 Fradd747 commented Mar 24, 2021

My god thank you. Have a nice day!

@unikforceit

This comment has been minimized.

Copy link

@unikforceit unikforceit commented May 12, 2021

it's really working good..

@soymipagina

This comment has been minimized.

Copy link

@soymipagina soymipagina commented May 22, 2021

Very very nice! Thank you.

@unikforceit

This comment has been minimized.

Copy link

@unikforceit unikforceit commented May 26, 2021

it's really working good..

only need to update the code 52: $post = get_page($page_for_posts_id); to new function $post = get_post($page_for_posts_id);

@BlazeIsClone

This comment has been minimized.

Copy link

@BlazeIsClone BlazeIsClone commented Sep 30, 2021

Really nice script still works like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment