Created
February 16, 2016 19:59
-
-
Save kingkool68/bc827d6b0dd67168a2ee to your computer and use it in GitHub Desktop.
An example of how to write your own breadcrumbs function in WordPress
This file contains hidden or 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
function goddard_breadcrumbs( $post_id = FALSE ) { | |
global $post; | |
if( $post_id ) { | |
$post = get_post( $post_id ); | |
} | |
if( !$post && !is_404() && !is_search() ) { | |
return false; | |
} | |
$crumbs = array( | |
array( | |
'label' => 'Home', | |
'url' => get_site_url(), | |
'current' => false | |
) | |
); | |
if( is_page() ) { | |
$page_crumbs = array( | |
array( | |
'label' => $post->post_title, | |
'url' => get_permalink( $post->ID ), | |
'current' => true | |
) | |
); | |
$parent = $post; | |
while( $parent->post_parent != 0 ) { | |
$parent = get_page( $parent->post_parent ); | |
$page_crumbs[] = array( | |
'label' => $parent->post_title, | |
'url' => get_permalink( $parent->ID ), | |
'current' => false | |
); | |
} | |
$page_crumbs = array_reverse( $page_crumbs ); | |
$crumbs = array_merge($crumbs, $page_crumbs); | |
} elseif( is_category() ) { | |
$term = get_queried_object(); | |
$crumbs[] = array( | |
'label' => $term->name, | |
'current' => true | |
); | |
} elseif( is_author() ) { | |
$crumbs[] = array( | |
'label' => get_the_author(), | |
'current' => true | |
); | |
} elseif( is_single() && $post->post_type == 'people' ) { | |
$data = get_goddard_people_options(); | |
$crumb_title = $data['first_name'] . ' ' . $data['last_name']; | |
if( $data['suffix'] ) { | |
$crumb_title .= ', ' . $data['suffix']; | |
} | |
$crumbs[] = array( | |
'label' => 'People', | |
'url' => get_site_url() . '/people/', | |
); | |
$crumbs[] = array( | |
'label' => $crumb_title, | |
'current' => true | |
); | |
} elseif( is_single() && $post->post_type == 'academics' ) { | |
$crumbs[] = array( | |
'label' => 'Academics', | |
'url' => get_site_url() . '/academics/', | |
); | |
$academic_crumbs = array( | |
array( | |
'label' => $post->post_title, | |
'current' => true | |
) | |
); | |
while( $post->post_parent > 0 ) { | |
$post = get_post( $post->post_parent ); | |
$academic_crumbs[] = array( | |
'label' => $post->post_title, | |
'url' => get_permalink( $post->ID ) | |
); | |
} | |
$crumbs = array_merge($crumbs, array_reverse( $academic_crumbs ) ); | |
} elseif( is_single() && $post->post_type == 'tribe_events' ) { | |
$crumbs[] = array( | |
'label' => 'Events', | |
'url' => tribe_get_events_link() | |
); | |
$crumbs[] = array( | |
'label' => get_the_title(), | |
'current' => true | |
); | |
} elseif( is_single() && $post->post_type == 'post' ) { | |
$post_crumbs = array(); | |
$cats = wp_get_object_terms( $post->ID, 'category' ); | |
foreach( $cats as $cat ) { | |
$post_crumbs[] = array( | |
'label' => $cat->name, | |
'url' => get_term_link( $cat ), | |
'current' => false | |
); | |
} | |
$post_crumbs[] = array( | |
'label' => apply_filters('the_title', $post->post_title), | |
'url' => get_permalink( $post->ID ), | |
'current' => true | |
); | |
$crumbs = array_merge($crumbs, $post_crumbs); | |
} | |
$output = '<ol class="breadcrumbs">'; | |
$slash = ' <span class="slash" aria-hidden="true">/</span>'; | |
foreach( $crumbs as $crumb ) { | |
$output .= '<li>'; | |
if( !$crumb['current'] || !isset( $crumb['current'] ) ) { | |
$output .= '<a href="' . $crumb['url'] . '">' . $crumb['label'] . '</a>'; | |
$output .= $slash; | |
} else { | |
$output .= $crumb['label']; | |
} | |
$output .= '</li>'; | |
} | |
$output .= '</ol>'; | |
echo $output; | |
//Reset $post back to the original query... | |
wp_reset_postdata(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment