Skip to content

Instantly share code, notes, and snippets.

@gyrus
Created June 18, 2013 22:47
Show Gist options
  • Save gyrus/5810204 to your computer and use it in GitHub Desktop.
Save gyrus/5810204 to your computer and use it in GitHub Desktop.
WordPress breadcrumbs output
/**
* Output breadcrumbs
*
* A lot of options for formatting, but this just uses the <nav> element, plus the
* 'breadcrumb' class as per @link http://microformats.org/wiki/blog-post-formats
*
* @uses $post
* @uses is_404()
* @uses apply_filters()
* @uses pilau_breadcrumb_link()
* @uses is_single()
* @uses get_post_type()
* @uses get_option()
* @param string $prefix
* @param string $sep
* @return void
*/
function pilau_breadcrumbs( $prefix = 'You are here: ', $sep = ' <span class="sep">/</span> ' ) {
global $post;
$output = '';
// Where are we now?
if ( is_404() ) {
// 404 not found
$output = $sep . __( 'Page not found' );
} else {
/*
* Work backwards from unlinked current title
* Need to use get_queried_object because get_the_title() on post listing pages returns
* the title of the last post in the loop.
*/
$output = $sep . apply_filters( 'the_title', get_queried_object()->post_title );
if ( $post->ancestors ) {
// For pages and anything else ancestors
foreach ( $post->ancestors as $ancestor_id ) {
$output = $sep . pilau_breadcrumb_link( $ancestor_id ) . $output;
}
} else if ( is_single() ) {
// Single post
if ( get_post_type() == 'post' ) {
// Standard post
$output = $sep . pilau_breadcrumb_link( get_option( 'page_for_posts' ) ) . $output;
} else if ( $cpt_structure_settings = get_option( 'sc_cpt_sh_permalinks' ) ) {
// Custom post type, get settings for CPT Structure
if ( in_array( get_post_type(), array_keys( $cpt_structure_settings ) ) )
$output = $sep . pilau_breadcrumb_link( $cpt_structure_settings[ get_post_type() ] ) . $output;
}
}
}
// Add home page - it's assumed breadcrumb will never be on home page
$output = '<a href="/">' . __( 'Home' ) . '</a>' . $output;
// Prefix?
if ( $prefix )
$output = '<span class="prefix">' . $prefix . '</span>' . $output;
// Output
echo '<nav class="breadcrumb"><p>' . $output . '</p></nav>';
}
/**
* Generate breadcrumb link
*
* @uses get_permalink()
* @uses get_the_title()
* @param int $id
* @return string
*/
function pilau_breadcrumb_link( $id ) {
return '<a href="' . get_permalink( $id ) . '">' . get_the_title( $id ) . '</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment