Last active
August 29, 2015 14:00
-
-
Save kevinruscoe/11395094 to your computer and use it in GitHub Desktop.
Breadcrumb functions for WordPress
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 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