亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

WordPress免插件實現面包屑導航的示例代碼

瀏覽:87日期:2022-06-01 18:04:26

你如果在開發自己的wordpress主題,想加入面包屑導航,而又不想使用插件的話,下面的代碼對你有幫助,這里提供了網上較為流行的兩種代碼,一是功能非常完善的,一是一個較為簡潔的代碼。

前面介紹了一種方法,你也可以嘗試一下,在文末給大家詳細介紹了自定義函數實現wordpress面包屑導航的代碼,可以點擊查看下。

功能非常完善代碼

1、將下面的代碼添加到主題的 functions.php

/** * WordPress 添加面包屑導航 * http://www.511yj.com/wordpress-add-breadcrumb.html */function cmp_breadcrumbs() { $delimiter = ’»’; // 分隔符 $before = ’<span class='current'>’; // 在當前鏈接前插入 $after = ’</span>’; // 在當前鏈接后插入 if ( !is_home() && !is_front_page() || is_paged() ) { echo ’<div itemscope itemtype='http://schema.org/WebPage' id='crumbs'>’.__( ’當前位置:’ , ’cmp’ ); global $post; $homeLink = home_url(); echo ’ <a itemprop='breadcrumb' href='http://www.aoyou183.cn/bcjs/’ . $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='http://www.aoyou183.cn/bcjs/’ . get_year_link(get_the_time(’Y’)) . ’' rel='external nofollow' rel='external nofollow' >’ . get_the_time(’Y’) . ’</a> ’ . $delimiter . ’ ’; echo ’<a itemprop='breadcrumb' href='http://www.aoyou183.cn/bcjs/’ . 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='http://www.aoyou183.cn/bcjs/’ . 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='http://www.aoyou183.cn/bcjs/’ . $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='http://www.aoyou183.cn/bcjs/’ . 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='http://www.aoyou183.cn/bcjs/’ . 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() ) { // 搜索結果 echo $before ; printf( __( ’Search Results for: %s’, ’cmp’ ), get_search_query() ); echo $after; } elseif ( is_tag() ) { //標簽 存檔 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>’; }}

前臺調用

<?php if(function_exists(’cmp_breadcrumbs’)) cmp_breadcrumbs();?>

下面看下自定義函數實現wordpress面包屑導航的代碼

面包屑導航 一是方便讀者所在的位置,更重要的是對SEO非常友好,利于蜘蛛知道你網站的目錄結構,所以給我們的wordpress主題添加面包屑導航是必須的。

1、在functioss.php添加以下代碼

/** * WordPress 添加面包屑導航 * 面包屑導航,直接輸出(echo) * Breadcrumb Trail * @param string $sep 導航對象分隔符,默認為’ > ’ */function bread_nav($sep = ’ > ’){ echo ’<div class='col-md-12 '><span class='glyphicon glyphicon-home text-primary'></span> 您當前的位置: <a href='http://www.aoyou183.cn/bcjs/’. home_url() .’'>首頁</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) .’正文內容 ’. get_the_title(); } echo ’</div>’;}

2、前臺調用

<?php bread_nav();?>

總結

到此這篇關于WordPress免插件實現面包屑導航的示例代碼的文章就介紹到這了,更多相關WordPress面包屑導航內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: word
相關文章:
主站蜘蛛池模板: 日韩精品你懂的在线播放 | 在线观看视频91 | 成人国产亚洲欧美成人综合网 | 极品嫩模被黑人20厘米 | 国产一级一片免费播放刺激 | 亚洲综合欧美 | 色婷婷狠狠久久综合五月 | 久久成人免费大片 | 黄色免费大全 | 色狠狠成人综合网 | 91音影 | 国产免费一区二区三区免费视频 | 日韩欧美亚洲一区精选 | 免费操片 | 青草视频在线免费 | 久久精品免费播放 | 开心激情站 | 999国产高清在线精品 | 日本欧美高清 | yyyyyy高清成人观看 | 国产影片中文字幕 | 久本草在线中文字幕亚洲欧美 | 国产毛片不卡 | 久久香蕉精品成人 | 久久99精品久久久久久h | 亚洲欧美日韩中文高清一 | 国产v精品欧美精品v日韩 | 男女激情视频国产免费观看 | 在线播放成人高清免费视频 | 站长推荐精品午夜免费影院 | 99国产精品九九视频免费看 | 特级做a爰片毛片免费看一区 | 日韩精品a | 天天天天鲁天天拍一拍 | 思思久久q6热在精品国产 | 亚洲色播永久网址大全 | 成人夜色视频在线观看网站 | 国产三级观看久久 | 国产乡下三级全黄三级带 | 国产精品久久久久999 | 免费国产黄色片 |