面包屑導(dǎo)航這個(gè)名字聽起來(lái)就有些古怪,簡(jiǎn)單的說(shuō)它就是提供給用戶回溯到網(wǎng)站首頁(yè)或入口頁(yè)面的一條快速路徑。本文說(shuō)下為 wordpress 添加面包屑導(dǎo)航的兩種方法。
什么是面包屑導(dǎo)航?
面包屑通常出現(xiàn)在頁(yè)面頂部,一般會(huì)位于標(biāo)題或頁(yè)頭的下方。它提供給用戶返回之前任何一個(gè)頁(yè)面的鏈接(這些鏈接也是能到達(dá)當(dāng)前頁(yè)面的路徑),在層級(jí)架構(gòu)中通常是這個(gè)頁(yè)面的父級(jí)頁(yè)面。
也可以這樣理解,面包屑提供給用戶回溯到網(wǎng)站首頁(yè)或入口頁(yè)面的一條快速路徑,它們絕大部分看起來(lái)就像這樣:首頁(yè)→分類頁(yè)→次級(jí)分類頁(yè)。如下圖所示:
Wordpress 面包屑導(dǎo)航顯示效果
面包屑導(dǎo)航的一些好處
1.可以提供多路徑的交互方式,方便用戶跳轉(zhuǎn)到其它頁(yè)面。在頁(yè)面及分類多的網(wǎng)站中尤其有用。
2.面包屑導(dǎo)航信息結(jié)構(gòu)對(duì)于網(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)然,可以通過(guò)插件來(lái)實(shí)現(xiàn)。本文主要說(shuō)的是傳統(tǒng)的代碼方法。
方法一:直接在相關(guān)頁(yè)面添加代碼即可實(shí)現(xiàn)面包屑導(dǎo)航
把以下代碼直接添加到你想出現(xiàn)面包屑導(dǎo)航的位置
12345678910111213141516171819202122232425<div class="mbx-dh">當(dāng)前位置:<a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a> »<?phpif( 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 頁(yè)面的導(dǎo)航標(biāo)題上面,你有可能需要添加的頁(yè)面可能有:archive.php、archives.php、links.php、page.php。
這個(gè)方法是轉(zhuǎn)載萬(wàn)戈同學(xué)的,原文請(qǐng)看這里。
方法二:通過(guò) functions.php 調(diào)用實(shí)現(xiàn)面包屑導(dǎo)航效果
首先把以下代碼添加到主題的 functions.php 文件中:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091function 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)用:
123<div class="mbx-dh"><?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?> </div>
如果想要美化下顯示方式,直接通過(guò)添加 css 即可。園子使用的 CSS 如下:
1.mbx-dh {padding: 5px 10px;}
以上提供的兩種方法,你可以根據(jù)需要任選一種都可實(shí)現(xiàn)。想折騰的朋友請(qǐng)開始吧。