麪包屑導航在 SEO 與用户體驗中都有着很大的優化,麪包屑導航能讓用户瞭解當前所處位置,以及當前頁面在整個網站中的位置。體現了網站的架構層級,能夠幫助用户快速學習和了解網站內容和組織方式,從而形成很好的位置感。提供返回各個層級的快速入口,方便用户操作。 Google 已經將麪包屑導航整合到搜索結果裏面,因此優化麪包屑導航每個層級的名稱,多使用關鍵字,都可以實現 SEO 優化。麪包屑路徑,對於提高用户體驗來説,是很有幫助的。方便用户,麪包屑主要用於為用户提供導航一個網站的次要方法,通過為一個大型多級網站的所有頁面提供麪包屑路徑,用户可以更容易的定位到上一次目錄,引導用户通行,減少返回到上一級頁面的點擊或操作,不用使用瀏覽器的 「返回」 按鈕或網站的主要導航來返回到上一級頁面。不用常常佔用屏幕空間,因為它們通常是水平排列以及簡單的樣式,麪包屑路徑不會佔用頁面太多的空間。降低跳出率,麪包屑路徑會是一個誘惑首次訪問者在進入一個頁面後去瀏覽這個網站的非常好的方法。比如説,一個用户通過谷歌搜索到一個頁面,然後看到一個麪包屑路徑,這將會誘使用户點擊上一級頁面去瀏覽感興趣的相關主題。有利用百度蜘蛛對網站的抓取,蜘蛛直接沿着那個鏈走就可以了,很方便。麪包屑有利於網站內鏈的建設,用麪包屑大大增加了網站的內部連接,提高用户體驗。
好了扯了這麼多的麪包屑導航的好處,現在小編就來教大家在 WordPress 網站中如何來添加麪包屑導航。
1 、打開 functions.php 文件並加入以下代碼:
function dimox_breadcrumbs() {
$delimiter = '»';
$name = '首頁';
$currentBefore = '<span>';
$currentAfter = '</span>';
if ( !is_home() && !is_front_page() || is_paged() ) {
global $post;
$home = get_bloginfo('url');
echo '<a href="'%20.%20$home%20.%20'">' . $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 . '當前分類 '';
single_cat_title();
echo ''' . $currentAfter;
} elseif ( is_day() ) {
echo '<a href="'%20.%20get_year_link(get_the_time('Y'))%20.%20'">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo '<a href="'%20.%20get_month_link(get_the_time('Y'),get_the_time('m'))%20.%20'">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
echo $currentBefore . get_the_time('d') . $currentAfter;
} elseif ( is_month() ) {
echo '<a href="'%20.%20get_year_link(get_the_time('Y'))%20.%20'">' . 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="'%20.%20get_permalink($page->ID)%20.%20'">' . 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 . '當前標籤頁 '';
single_tag_title();
echo ''' . $currentAfter;
} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $currentBefore . '當前文章頁 ' . $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 ')';
}
}
}
然後在需要顯示麪包屑導航的地方加入以下代碼:
<?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?>
好了這樣就完成了 WordPress 的麪包屑導航。