public
Last active

Add Breadcrumb Navigation

  • Download Gist
add-breadcrumb-navigation.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<?php // Add Breadcrumb Navigation
function write_breadcrumb() {
$pid = $post->ID;
$trail = '<a href="/">'. __('Home', 'textdomain') .'</a>';
 
if (is_front_page()) {
// do nothing
}
elseif (is_page()) {
$bcarray = array();
$pdata = get_post($pid);
$bcarray[] = ' &raquo; '.$pdata->post_title."\n";
while ($pdata->post_parent) {
$pdata = get_post($pdata->post_parent);
$bcarray[] = ' &raquo; <a href="'.get_permalink($pdata->ID).'">'.$pdata->post_title.'</a>';
}
$bcarray = array_reverse($bcarray);
foreach ($bcarray AS $listitem) {
$trail .= $listitem;
}
}
elseif (is_single()) {
$pdata = get_the_category($pid);
$adata = get_post($pid);
if(!empty($pdata)){
$data = get_category_parents($pdata[0]->cat_ID, TRUE, ' &raquo; ');
$trail .= " &raquo; ".substr($data,0,-8);
}
$trail.= ' &raquo; '.$adata->post_title."\n";
}
elseif (is_category()) {
$pdata = get_the_category($pid);
$data = get_category_parents($pdata[0]->cat_ID, TRUE, ' &raquo; ');
if(!empty($pdata)){
$trail .= " &raquo; ".substr($data,0,-8);
}
}
$trail.="";
return $trail;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.