Skip to content

Instantly share code, notes, and snippets.

@tinotriste
Last active February 6, 2024 08:53
Show Gist options
  • Star 64 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save tinotriste/5387124 to your computer and use it in GitHub Desktop.
Save tinotriste/5387124 to your computer and use it in GitHub Desktop.
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
Copy link

ddcddc commented Aug 23, 2017

Thanks!!

Copy link

ghost 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
Copy link

29lim85 commented Jul 7, 2018

Time saver ! ! !

@carlosjulian
Copy link

Thanks!!! easy of implement to my web.

@shihning
Copy link

Thank You!!

@wh4t3ver92
Copy link

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
Copy link

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
Copy link

@MediaSmackShahzad
Copy link

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

@boutmos
Copy link

boutmos commented Sep 9, 2020

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

@mikemike
Copy link

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.

For those looking for a more thorough implementation please take a look at how to handle breadcrumbs in WordPress. It includes most of what this gist does, but then also includes various missing features for handling custom post types, parent pages and doesn't use any deprecated functions. Definitely recommend.

@boutmos
Copy link

boutmos commented Nov 10, 2020

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

@joshbali
Copy link

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

@unikforceit
Copy link

it's really working good..

@soymipagina
Copy link

Very very nice! Thank you.

@unikforceit
Copy link

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
Copy link

Really nice script still works like a charm!

@valentin-grenier
Copy link

Thanks a lot !!

@farbodakvan
Copy link

Thanks :)

@mikemike
Copy link

mikemike commented May 2, 2023

For those looking for a more thorough implementation please take a look at how to handle breadcrumbs in WordPress. It includes most of what this gist does, but then also includes various missing features for handling custom post types, parent pages and doesn't use any deprecated functions. Definitely recommend.

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