纯代码给博客添加面包屑导航

面包屑导航这个概念来自童话故事《汉赛尔和格莱特》,当汉赛尔和格莱特穿过森林时,不小心迷路了,但是他们发现在沿途走过的地方都撒下了面包屑,让这些面包屑来帮助他们找到回家的路,所以,面包屑导航的作用是告诉访问者他们目前在网站中的位置以及如何返回,面包屑导航对于你的站点来说虽然不是重要的组成部分,但面包屑路径,对于提高用户体验来说,是很有帮助的。下面就记录一下纯代码添加面包屑导航。

1、functions.php文件修改
主题的functions.php中最后一个 ?> 之前添加如下代码:
[sourcecode]
/* breadcrumbs nav */
function dimox_breadcrumbs() {

$delimiter = ‘»’;
$name = ‘Home’; //text for the ‘Home’ link
$currentBefore = ‘<span>’;
$currentAfter = ‘</span>’;

if ( !is_home() && !is_front_page() || is_paged() ) {

echo ‘<div id="crumbs">’;

global $post;
$home = get_bloginfo(‘url’);
echo ‘<a href="’ . $home . ‘">’ . $name . ‘</a> ‘ . $delimiter . ‘ ‘;

if ( is_category() ) {
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ‘ ‘ . $delimiter . ‘ ‘));
echo $currentBefore . ‘Archive by category &#39;’;
single_cat_title();
echo ‘&#39;’ . $currentAfter;

} elseif ( is_day() ) {
echo ‘<a href="’ . get_year_link(get_the_time(‘Y’)) . ‘">’ . get_the_time(‘Y’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo ‘<a href="’ . get_month_link(get_the_time(‘Y’),get_the_time(‘m’)) . ‘">’ . get_the_time(‘F’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $currentBefore . get_the_time(‘d’) . $currentAfter;

} elseif ( is_month() ) {
echo ‘<a href="’ . get_year_link(get_the_time(‘Y’)) . ‘">’ . get_the_time(‘Y’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $currentBefore . get_the_time(‘F’) . $currentAfter;

} elseif ( is_year() ) {
echo $currentBefore . get_the_time(‘Y’) . $currentAfter;

} elseif ( is_single() ) {
$cat = get_the_category(); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
echo $currentBefore;
the_title();
echo $currentAfter;

} elseif ( is_page() && !$post->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter;

} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = ‘<a href="’ . get_permalink($page->ID) . ‘">’ . get_the_title($page->ID) . ‘</a>’;
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ‘ ‘ . $delimiter . ‘ ‘;
echo $currentBefore;
the_title();
echo $currentAfter;

} elseif ( is_search() ) {
echo $currentBefore . ‘Search results for &#39;’ . get_search_query() . ‘&#39;’ . $currentAfter;

} elseif ( is_tag() ) {
echo $currentBefore . ‘Posts tagged &#39;’;
single_tag_title();
echo ‘&#39;’ . $currentAfter;

} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $currentBefore . ‘Articles posted by ‘ . $userdata->display_name . $currentAfter;

} elseif ( is_404() ) {
echo $currentBefore . ‘Error 404’ . $currentAfter;
}

if ( get_query_var(‘paged’) ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ‘ (‘;
echo __(‘Page’) . ‘ ‘ . get_query_var(‘paged’);
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ‘)’;
}

echo ‘</div>’;

}
}//End: breadcrumbs
[/sourcecode]

2、调用
适当的位置(比如single.php文件中)添加调用
[sourcecode]
<?php if (function_exists(‘dimox_breadcrumbs’)) dimox_breadcrumbs(); ?>
[/sourcecode]

3、CSS修改
style.css文件中增加如下几行:
[sourcecode]
#crumbs{height:24px;line-height:20px;margin:0 -20px 20px 10px;border-bottom:1px solid #dfdfdf;text-shadow:0 1px 0 #ddd;}
#crumbs a{color:#999;}
#crumbs a:hover{color:#4a630f;}
[/sourcecode]

效果就像我的博客显示的这样。
插件的话可以考虑Breadcrumb NavXT。

发表评论

电子邮件地址不会被公开。 必填项已用*标注