Instantly share code, notes, and snippets.
Created
April 9, 2015 23:53
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save bobbiejwilson/8b164911ab4b75b7f317 to your computer and use it in GitHub Desktop.
WordPress Breadcrumb function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wds_breadcrumbs() { | |
global $post, $paged; | |
if ( !is_home() || !is_front_page() || !is_paged() ) { | |
echo '<a href="' . esc_url( get_home_url() ) . '" class="crumb-home"><span class="screen-reader-text">' . __( 'Home', 'wds' ) . '</span></a>'; | |
if ( is_category() ) { | |
$category = get_the_category(); | |
if ( $category ) { | |
foreach ( $category as $category ) { | |
echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>'; | |
} | |
} | |
sprintf( __( 'Posts filed under <q>%s</q>', 'wds' ), single_cat_title( '', false ) ); | |
} elseif ( is_day() ) { | |
echo '<li><span><a href="' . get_year_link( get_the_time('Y') ) . '">' . get_the_time('Y') . '</a></span></li>'; | |
echo '<li><span><a href="' . get_month_link( get_the_time('Y'), get_the_time('m') ) . '">' . get_the_time('F') . '</a></span></li>'; | |
echo '<li class="active"><span>' . get_the_time('d') . '</span></li>'; | |
} elseif ( is_month() ) { | |
echo '<li><span><a href="' . get_year_link( get_the_time('Y') ) . '">' . get_the_time('Y') . '</a></span></li>'; | |
echo '<li class="active"><span>' . get_the_time('F') . '</span></li>'; | |
} elseif ( is_year() ) { | |
echo '<li class="active"><span>' . get_the_time('Y') . '</span></li>'; | |
} elseif ( is_single() && !is_attachment() ) { | |
if ( get_post_type() != 'post' ) { | |
$post_type = get_post_type_object( get_post_type() ); | |
$slug = $post_type->rewrite; | |
echo '<li><span><a href="' . home_url() . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a></span></li>'; | |
echo '<li class="active"><span>' . get_the_title() . '</span></li>'; | |
} else { | |
$category = get_the_category(); | |
if ( $category ) { | |
foreach ( $category as $category ) { | |
echo '<li><span><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></span></li>'; | |
} | |
} | |
echo '<li class="active"><span>' . get_the_title() . '</span></li>'; | |
} | |
} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) { | |
$post_type = get_post_type_object( get_post_type() ); | |
echo '<li class="active"><span>' . $post_type->labels->singular_name . '</span></li>'; | |
} elseif ( is_attachment() ) { | |
$parent = get_post( $post->post_parent ); | |
$category = get_the_category( $parent->ID ); | |
if ( $category ) { | |
foreach ( $category as $category ) { | |
echo '<li><span><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></span></li>'; | |
} | |
} | |
echo '<li class="active"><span>' . get_the_title() . '</span></li>'; | |
} elseif ( is_page() && !$post->post_parent ) { | |
echo '<li class="active"><span>' . get_the_title() . '</span></li>'; | |
} elseif ( is_page() && $post->post_parent ) { | |
$parent_id = $post->post_parent; | |
$breadcrumbs = array(); | |
while ( $parent_id != 0 ) { | |
$page = get_post($parent_id); | |
$breadcrumbs[] = '<li><span><a href="' . get_permalink($page->ID) . '">' . get_the_title( $page->ID ) . '</a></span></li>'; | |
//Get next parent, walk backwards | |
$parent_id = $page->post_parent; | |
} | |
$breadcrumbs = array_reverse( $breadcrumbs ); | |
foreach ( $breadcrumbs as $crumb ) { | |
echo wp_kses( $crumb, $accepted_parameters ); | |
} | |
echo '<li class="active"><span>' . get_the_title() . '</span></li>'; | |
} elseif ( is_search() ) { | |
echo '<li class="active"><span>' . sprintf( __( 'Search results for <q>%s</q>', 'museum-core' ), esc_attr( get_search_query() ) ) . '</span></li>'; | |
} elseif ( is_tag() ) { | |
echo '<li class="active"><span>' . sprintf( __( 'Posts tagged <q>%s</q>', 'museum-core' ), single_tag_title( '', false ) ) . '</span></li>'; | |
} elseif ( is_author() ) { | |
global $author; | |
echo '<li class="active"><span>' . sprintf( __( 'All posts by %s', 'museum-core' ), get_the_author_meta('display_name',$author) ) . '</span></li>'; | |
} elseif ( is_404() ) { | |
echo '<li class="active"><span>' . __( 'Error 404', 'museum-core' ) . '</span></li>'; | |
} | |
} | |
if ( is_paged() ) { | |
$front_page_ID = get_option( 'page_for_posts' ); | |
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) { | |
echo ' <span class="active paged">(' . sprintf( __( 'Page %s', 'museum-core' ), esc_attr( $paged ) ) . ')</li>'; | |
} else { | |
echo '<li><span><a href="' . esc_url( get_home_url() ) . '">' . __( 'Home', 'museum-core' ) . '</a></span></li>'; | |
echo '<li><span><a href="' . esc_url( get_home_url() ) . '/?p=' . $front_page_ID . '">' . __( 'Blog', 'museum-core' ) . '</a></span></li>'; | |
echo '<li class="active paged">' . sprintf( __( 'Page %s', 'museum-core' ), esc_attr( $paged ) ) . '</li>'; | |
} | |
} | |
echo '</ul>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment