WordPress 添加面包屑导航的三种方法

2014年05月30日 WordPress 没人吐槽 10602次浏览
博客主机

面包屑导航,简单的说它就是提供给用户回溯到网站首页或入口页面的一条快速路径。今天倡萌分享一下WordPress 添加面包屑导航的三种方法,希望对大家有所帮助,原文参考自园子博客。

什么是面包屑导航

面包屑通常出现在页面顶部,一般会位于标题或页头的下方。它提供给用户返回之前任何一个页面的链接(这些链接也是能到达当前页面的路径),在层级架构中通常是这个页面的父级页面。

也可以这样理解,面包屑提供给用户回溯到网站首页或入口页面的一条快速路径,它们绝大部分看起来就像这样:首页→分类页→次级分类页。如下图所示:

2011-03-29_02396

面包屑导航的好处

1.可以提供多路径的交互方式,方便用户跳转到其它页面。在页面及分类多的网站中尤其有用。

2.面包屑导航信息结构对于网站的seo也有着大的好处,它可以更多的强调网站关键字,扩大关键字的范围,从而达到更好的优化目的。

3.它从一个侧面展示了该信息集合的信息结构和集合方式,可以让用户在最快的时间之内找到需要的东西。

添加面包屑导航的方法

方法一:直接在相关页面添加代码

把以下代码直接添加到你想出现面包屑导航的位置,比如 header.php 里面,也可以放在 single.php 页面的导航标题上面,你有可能需要添加的页面可能有:archive.php、archives.php、links.php、page.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
<div class="mbx-dh">
当前位置:<a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a> &raquo;
<?php
if( is_single() ){
$categorys = get_the_category();
$category = $categorys[0];
echo( get_category_parents($category->term_id,true,' &raquo; ') );
the_title();
} elseif ( is_page() ){
the_title();
} elseif ( is_category() ){
single_cat_title();
} elseif ( is_tag() ){
single_tag_title();
} elseif ( is_day() ){
the_time('Y年Fj日');
} elseif ( is_month() ){
the_time('Y年F');
} elseif ( is_year() ){
the_time('Y年');
} elseif ( is_search() ){
echo $s.' 的搜索结果';
}
?>
</div>

此法来自万戈博客,原文链接

方法二:通过 functions.php 调用

首先把以下代码添加到主题的 functions.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function dimox_breadcrumbs() {
 
  $delimiter = '&raquo;';
  $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 '' . $name . ' ' . $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 '' . get_the_time('Y') . ' ' . $delimiter . ' ';
      echo '' . get_the_time('F') . ' ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('d') . $currentAfter;
 
    } elseif ( is_month() ) {
      echo '' . get_the_time('Y') . ' ' . $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[] = '' . get_the_title($page->ID) . '';
        $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>';
 
  }
}

最后在适当的地方(如方法一中提到的几个文件)添加以下代码调用

1
2
3
<div class="mbx-dh">
<?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?> 
</div>

如果想要美化下显示方式,直接通过添加 css 即可。

1
.mbx-dh {padding: 5px 10px;}

方法三:使用Breadcrumb NavXT插件

在后台,添加新插件,搜索“Breadcrumb NavXT” 安装、激活。然后在header.php中合适的地方增加代码:

1
2
3
<div class="breadcrumbs"> 
<?php if(function_exists('bcn_display')) {     bcn_display(); } ?> 
</div>

这个插件还可以设置,进入设置选项后,共有general、current item、posts&pages、categories、tags、date archives、miscellaneous等相关设置。

1.general设置(一般设置)

可以设置面包屑导航的分割符(Breadcrumb Separator)

Breadcrumb Max Title Length——设置面包屑导航标题的最大长度

Home Breadcrumb——主页的标题

Place the home breadcrumb in the trail:如果打勾的话表示在面包屑导航中显示主页,不打勾的话则不显示。

Home Title——填入主页的显示标题,默认是blog,你可以修改为你想要的标题。

Home Prefix——(主页前缀)可以在主页前面添加说明文字。

Home Suffix——(主页后缀)在主页后面添加说明文字

Home Anchor、Blog Anchor一般不需要修改,插件会自动生成链接。

2.current item设置

Link Current Item表示是否设置当前页面链接

Current Item Prefix和Current Item Suffix :当前页的前后缀

current Item Anchor默认就可以了

Paged Breadcrumb、Paged Prefix、Paged Suffix是用来设置页面的面包屑导航,设置方法同上。

3.posts&pages设置

Post Prefix、Post Suffix、Post Anchor、Page Prefix、Page Suffix、Attachment Prefix等选项的基本上设置方法同上

但是,

Post Taxonomy Display如果勾选的话表示在按照主页-分类-子分类-文章等形式显示出来,如果不勾选的话表示只分按照主页-文章显示出来。

Post Taxonomy表示按照什么来进行导航,有分类目录和标签可以选择。

4.categories设置

基本上设置方法同上

5.date archives这里可以修改文章日期分类面包屑导航显示样式。

6.miscellaneous选项里提供了Author、Search、404页面的面包屑导航设置。

转载自网络:http://www.cmhello.com/WordPress-breadcrumbs.html

博客主机

给我留言