Capn Code
Member
Posted 1 year ago #
For those wishing to use a non-plugin based breadcrumb trail, simply insert the following code in functions:
function breadcrumb() {
// Check if Permalinks are enabled
$breadcrumb = array();
// Get current post
$current = $post->ID;
// Check if current post has ancestors
if($GLOBALS['post']->ancestors) {
$ancestors = $GLOBALS['post']->ancestors;
// Step through ancestors array to build breadcrumb
foreach($ancestors as $i => $text)
{
$breadcrumb[$i] = '<a href="' . get_page_link($text) . '" title="' . attribute_escape(apply_filters('the_title', $text->post_title)) . '">'.get_the_title($text).'</a>';}
}
// Insert a link to home
array_unshift($breadcrumb, '<a href="' . get_option('home') . '" alt="Home" title="Home">Home</a>');
$breadcrumb[] .= '<a href="' . get_page_link($current->ID) . '" title="' . attribute_escape(apply_filters('the_title', $current->post_title)) . '">'.get_the_title($current).'</a>';
// Display breacrumb with demarcator
echo implode(' > ', $breadcrumb);
}
Capn Code
Member
Posted 1 year ago #
I jumped the gun a little there. Here's a more robust version:
$breadcrumb = array();
// Get current page
global $wp_query;
$current = $wp_query->post;
// Check if current post has ancestors
if($current->ancestors) {
$ancestors = array_reverse($current->ancestors);
// Step through ancestors array to build breadcrumb
foreach($ancestors as $i => $text)
{
$breadcrumb[$i] = '<a href="' . get_page_link($text) . '" title="' . attribute_escape(apply_filters('the_title', $text->post_title)) . '">'.ucfirst(strtolower(get_the_title($text))).'</a>';
}
}
// Insert a link to the current page
$breadcrumb[] = '<a href="' . get_page_link($current->ID) . '" title="' . attribute_escape(apply_filters('the_title', $current->post_title)) . '">'.ucfirst(strtolower(get_the_title($current))).'</a>';
// Insert a link to home
array_unshift($breadcrumb, '<a href="' . get_option('home') . '" alt="Home" title="Home">Home</a>');
// Display breacrumb with demarcator
echo implode(' > ', $breadcrumb);
}