面包屑導(dǎo)航這個(gè)名字聽起來就有些古怪,簡單的說它就是提供給用戶回溯到網(wǎng)站首頁或入口頁面的一條快速路徑。本文說下為 wordpress 添加面包屑導(dǎo)航的兩種方法。
什么是面包屑導(dǎo)航?
面包屑通常出現(xiàn)在頁面頂部,一般會位于標(biāo)題或頁頭的下方。它提供給用戶返回之前任何一個(gè)頁面的鏈接(這些鏈接也是能到達(dá)當(dāng)前頁面的路徑),在層級架構(gòu)中通常是這個(gè)頁面的父級頁面。
也可以這樣理解,面包屑提供給用戶回溯到網(wǎng)站首頁或入口頁面的一條快速路徑,它們絕大部分看起來就像這樣:首頁→分類頁→次級分類頁。如下圖所示:
 Wordpress 面包屑導(dǎo)航顯示效果
面包屑導(dǎo)航的一些好處
1.可以提供多路徑的交互方式,方便用戶跳轉(zhuǎn)到其它頁面。在頁面及分類多的網(wǎng)站中尤其有用。
2.面包屑導(dǎo)航信息結(jié)構(gòu)對于網(wǎng)站的seo也有著大的好處,它可以更多的強(qiáng)調(diào)網(wǎng)站關(guān)鍵字,擴(kuò)大關(guān)鍵字的范圍,從而達(dá)到更好的優(yōu)化目的。
3.它從一個(gè)側(cè)面展示了該信息集合的信息結(jié)構(gòu)和集合方式,可以讓用戶在最快的時(shí)間之內(nèi)找到需要的東西。
當(dāng)然,可以通過插件來實(shí)現(xiàn)。本文主要說的是傳統(tǒng)的代碼方法。
方法一:直接在相關(guān)頁面添加代碼即可實(shí)現(xiàn)面包屑導(dǎo)航
把以下代碼直接添加到你想出現(xiàn)面包屑導(dǎo)航的位置
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">
當(dāng)前位置:<a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a> »
<?php
if( is_single() ){
$categorys = get_the_category();
$category = $categorys[0];
echo( get_category_parents($category->term_id,true,' » ') );
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.' 的搜索結(jié)果';
}
?>
</div> |
你可以把這些代碼添加到 header.php 里面,也可以放在 single.php 頁面的導(dǎo)航標(biāo)題上面,你有可能需要添加的頁面可能有:archive.php、archives.php、links.php、page.php。
這個(gè)方法是轉(zhuǎn)載萬戈同學(xué)的,原文請看這里。
方法二:通過 functions.php 調(diào)用實(shí)現(xiàn)面包屑導(dǎo)航效果
首先把以下代碼添加到主題的 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 = '»';
$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 '';
single_cat_title();
echo ''' . $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 '' . get_search_query() . ''' . $currentAfter;
} elseif ( is_tag() ) {
echo $currentBefore . 'Posts tagged '';
single_tag_title();
echo ''' . $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>';
}
} |
最后在適當(dāng)?shù)牡胤剑ㄈ绶椒ㄒ恢刑岬降膸讉€(gè)文件)添加以下代碼調(diào)用:
1
2
3
| <div class="mbx-dh">
<?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?>
</div> |
如果想要美化下顯示方式,直接通過添加 css 即可。園子使用的 CSS 如下:
1
| .mbx-dh {padding: 5px 10px;} |
以上提供的兩種方法,你可以根據(jù)需要任選一種都可實(shí)現(xiàn)。想折騰的朋友請開始吧。
|