Adjusting Wordpress Function to display Parent Category
I am trying to display the classic breadcrumbs on a WP based site. I have the following function,
//breadcrumb function
function the_breadcrumb() {
if (!is_home()) {
echo '<a href="';
echo get_option('home');
echo '">';
//bloginfo('name');
echo 'Home';
echo "</a> > ";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
echo " » ";
the_title();
}
} elseif (is_page()) {
echo the_title();
}
}
}
But when the page is inside a parent category (i.e., About (Parent), Advisors (Child)) it only shows the child page. Any thoughts on how I can add a condition to show the parent page as well? Any help would be greatly appreciated.
/* EDITED */ I found a perfect working function for it:
function breadcrumbTrail($crumbs = true, $title = 'Browse', $separator = '/')
{
global $post;
?>
<div class="breadcrumbs">
<a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name')开发者_如何转开发; ?>"><?php _e('Home','options'); ?></a> <?php echo $separator; ?>
<?php
if(is_single()) :
the_category(', '); echo ' ' . $separator . ' ';
elseif(is_page()) :
$parent_id = $post->post_parent;
$parents = array();
while($parent_id) :
$page = get_page($parent_id);
if($params["link_none"])
$parents[] = get_the_title($page->ID);
else
$parents[] = '<a href="'.get_permalink($page->ID).'" title="'.get_the_title($page->ID).'">'.get_the_title($page->ID).'</a> ' . $separator . ' ';
$parent_id = $page->post_parent;
endwhile;
$parents = array_reverse($parents);
foreach($parents as $val) :
echo $val;
endforeach;
endif;
the_title();
?>
</div>
<?php
}
Hope this helps someone out.
I suggest using the Breadcrumb NavXT plugin, it works not only for categories but also the pages with parent.
If you want to write your own customised code for the breadcrumb, you can see how Breadcrumb NavXT did it.
精彩评论