Skip to content

Instantly share code, notes, and snippets.

@joshbali
Forked from tinotriste/breadcrumbs-functions.php
Last active November 22, 2020 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbali/e697c56ff4c33ad4f09675eabddd7c68 to your computer and use it in GitHub Desktop.
Save joshbali/e697c56ff4c33ad4f09675eabddd7c68 to your computer and use it in GitHub Desktop.
Wordpress: Breadcrumbs function
<?php
/*=============================================
= BREADCRUMBS =
=============================================*/
// to include in functions.php
function the_breadcrumb() {
$sep = '<i class="fas fa-angle-double-right breadcrumb-icon"></i>';
if (!is_front_page()) {
/* Start the breadcrumb with a link to home */
echo 'You are here:&nbsp;&nbsp;<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() ){
/* Show Blog link on categories and single posts */
echo '<a href="';
echo get_permalink( get_option( 'page_for_posts' ) );
echo '">';
printf( __( '%s', 'text_domain' ), get_the_title( get_option('page_for_posts', true) ) );
echo '</a>';
echo $sep;
/* show categories and posts */
if( is_category() ) {
single_term_title();
} elseif (is_single() ) {
$cats = get_the_category( get_the_ID() );
$cat = array_shift($cats);
echo '<a href="' . esc_url( get_category_link( $cat->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $cat->name ) ) . '">'. $cat->name .'</a>';
}
} 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()) {
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_page($page_for_posts_id);
setup_postdata($post);
the_title();
rewind_posts();
}
}
}
}
/*
* Credit: http://www.thatweblook.co.uk/blog/tutorials/tutorial-wordpress-breadcrumb-function/
*/
?>
<!-- start breadcrumbs -->
<?php the_breadcrumb(); ?>
<!-- end breadcrumbs -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment