您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“WordPress無需插件怎么實(shí)現(xiàn)面包屑導(dǎo)航”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“WordPress無需插件怎么實(shí)現(xiàn)面包屑導(dǎo)航”吧!
你如果在開發(fā)自己的wordpress主題,想加入面包屑導(dǎo)航,而又不想使用插件的話,下面的代碼對你有幫助,這里提供了網(wǎng)上較為流行的兩種代碼,一是功能非常完善的,一是一個(gè)較為簡潔的代碼。
前面介紹了一種方法,你也可以嘗試一下,在文末給大家詳細(xì)介紹了自定義函數(shù)實(shí)現(xiàn)wordpress面包屑導(dǎo)航的代碼,可以點(diǎn)擊查看下。
功能非常完善代碼
1、將下面的代碼添加到主題的 functions.php :
/** * WordPress 添加面包屑導(dǎo)航 * http://www.511yj.com/wordpress-add-breadcrumb.html */ function cmp_breadcrumbs() { $delimiter = '»'; // 分隔符 $before = '<span class="current">'; // 在當(dāng)前鏈接前插入 $after = '</span>'; // 在當(dāng)前鏈接后插入 if ( !is_home() && !is_front_page() || is_paged() ) { echo '<div itemscope itemtype="http://schema.org/WebPage" id="crumbs">'.__( '當(dāng)前位置:' , 'cmp' ); global $post; $homeLink = home_url(); echo ' <a itemprop="breadcrumb" href="' . $homeLink . '" rel="external nofollow" >' . __( '無作為' , 'cmp' ) . '</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){ $cat_code = get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '); echo $cat_code = str_replace ('<a','<a itemprop="breadcrumb"', $cat_code ); } echo $before . '' . single_cat_title('', false) . '' . $after; } elseif ( is_day() ) { // 天 存檔 echo '<a itemprop="breadcrumb" href="' . get_year_link(get_the_time('Y')) . '" rel="external nofollow" rel="external nofollow" >' . get_the_time('Y') . '</a> ' . $delimiter . ' '; echo '<a itemprop="breadcrumb" href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '" rel="external nofollow" >' . get_the_time('F') . '</a> ' . $delimiter . ' '; echo $before . get_the_time('d') . $after; } elseif ( is_month() ) { // 月 存檔 echo '<a itemprop="breadcrumb" href="' . get_year_link(get_the_time('Y')) . '" rel="external nofollow" rel="external nofollow" >' . get_the_time('Y') . '</a> ' . $delimiter . ' '; echo $before . get_the_time('F') . $after; } elseif ( is_year() ) { // 年 存檔 echo $before . get_the_time('Y') . $after; } elseif ( is_single() && !is_attachment() ) { // 文章 if ( get_post_type() != 'post' ) { // 自定義文章類型 $post_type = get_post_type_object(get_post_type()); $slug = $post_type->rewrite; echo '<a itemprop="breadcrumb" href="' . $homeLink . '/' . $slug['slug'] . '/" rel="external nofollow" >' . $post_type->labels->singular_name . '</a> ' . $delimiter . ' '; echo $before . get_the_title() . $after; } else { // 文章 post $cat = get_the_category(); $cat = $cat[0]; $cat_code = get_category_parents($cat, TRUE, ' ' . $delimiter . ' '); echo $cat_code = str_replace ('<a','<a itemprop="breadcrumb"', $cat_code ); echo $before . get_the_title() . $after; } } elseif ( !is_single() && !is_page() && get_post_type() != 'post' ) { $post_type = get_post_type_object(get_post_type()); echo $before . $post_type->labels->singular_name . $after; } elseif ( is_attachment() ) { // 附件 $parent = get_post($post->post_parent); $cat = get_the_category($parent->ID); $cat = $cat[0]; echo '<a itemprop="breadcrumb" href="' . get_permalink($parent) . '" rel="external nofollow" >' . $parent->post_title . '</a> ' . $delimiter . ' '; echo $before . get_the_title() . $after; } elseif ( is_page() && !$post->post_parent ) { // 頁面 echo $before . get_the_title() . $after; } elseif ( is_page() && $post->post_parent ) { // 父級頁面 $parent_id = $post->post_parent; $breadcrumbs = array(); while ($parent_id) { $page = get_page($parent_id); $breadcrumbs[] = '<a itemprop="breadcrumb" href="' . get_permalink($page->ID) . '" rel="external nofollow" >' . get_the_title($page->ID) . '</a>'; $parent_id = $page->post_parent; } $breadcrumbs = array_reverse($breadcrumbs); foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' '; echo $before . get_the_title() . $after; } elseif ( is_search() ) { // 搜索結(jié)果 echo $before ; printf( __( 'Search Results for: %s', 'cmp' ), get_search_query() ); echo $after; } elseif ( is_tag() ) { //標(biāo)簽 存檔 echo $before ; printf( __( 'Tag Archives: %s', 'cmp' ), single_tag_title( '', false ) ); echo $after; } elseif ( is_author() ) { // 作者存檔 global $author; $userdata = get_userdata($author); echo $before ; printf( __( 'Author Archives: %s', 'cmp' ), $userdata->display_name ); echo $after; } elseif ( is_404() ) { // 404 頁面 echo $before; _e( 'Not Found', 'cmp' ); echo $after; } if ( get_query_var('paged') ) { // 分頁 if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo sprintf( __( '( Page %s )', 'cmp' ), get_query_var('paged') ); } echo '</div>'; } }
前臺調(diào)用
<?php if(function_exists('cmp_breadcrumbs')) cmp_breadcrumbs();?>
下面看下自定義函數(shù)實(shí)現(xiàn)wordpress面包屑導(dǎo)航的代碼
面包屑導(dǎo)航 一是方便讀者所在的位置,更重要的是對SEO非常友好,利于蜘蛛知道你網(wǎng)站的目錄結(jié)構(gòu),所以給我們的wordpress主題添加面包屑導(dǎo)航是必須的。
1、在functioss.php添加以下代碼
/** * WordPress 添加面包屑導(dǎo)航 * 面包屑導(dǎo)航,直接輸出(echo) * Breadcrumb Trail * @param string $sep 導(dǎo)航對象分隔符,默認(rèn)為' > ' */ function bread_nav($sep = ' > '){ echo '<div class="col-md-12 "><span class="glyphicon glyphicon-home text-primary"></span> 您當(dāng)前的位置: <a href="'. home_url() .'" title="首頁">首頁</a>'; if ( is_category() ){ //如果是欄目頁面 global $cat; echo $sep . get_category_parents($cat, true, $sep) . '文章列表'; }elseif ( is_page() ){ //如果是自定義頁面 echo $sep . get_the_title(); }elseif ( is_single() ){ //如果是文章頁面 $categories = get_the_category(); $cat = $categories[0]; echo $sep . get_category_parents($cat->term_id, true, $sep) .'正文內(nèi)容 '. get_the_title(); } echo '</div>'; }
2、前臺調(diào)用
<?php bread_nav();?>
總結(jié)
到此,相信大家對“WordPress無需插件怎么實(shí)現(xiàn)面包屑導(dǎo)航”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。