Skip to content

Instantly share code, notes, and snippets.

@kevinruscoe
Last active August 29, 2015 14:00
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 kevinruscoe/11395094 to your computer and use it in GitHub Desktop.
Save kevinruscoe/11395094 to your computer and use it in GitHub Desktop.
Breadcrumb functions for WordPress
<?php
function the_breadcrumb(){
echo get_breadcrumb();
}
function get_breadcrumb(){
global $post;
$posts = array();
$current_post = get_post();
// Add home
$posts[] = array('title' => 'Home', 'permalink' => home_url());
// Loop parents
while( $current_post->post_parent ){
$current_post = get_post( $post->post_parent );
$posts[] = array('title' => $current_post->post_title, 'permalink' => get_permalink( $current_post->ID ) );
}
// Get this page
if( !is_front_page() ){
$posts[] = array('title' => get_the_title(), 'permalink' => null);
}
// Print HTML
$html = "<ul class='breadcrumb'>";
foreach( $posts as $breadcrumb_post ){
if( is_null($breadcrumb_post['permalink']) ){
$html .= "<li><span>" . ucfirst($breadcrumb_post['title']) . "</span></li>";
}else{
$html .= "<li><a href='" . $breadcrumb_post['permalink'] . "'>" . ucfirst($breadcrumb_post['title']) . "</a></li>";
}
}
$html .= "</ul>";
return $html;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment