激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

DEDECMS|帝國(guó)CMS|Discuz|PHPCMS|Wordpress|ZBLOG|ECSHOP|蘋(píng)果CMS|極致CMS|CMS系統(tǒng)|

服務(wù)器之家 - 建站程序 - Wordpress - WordPress免插件實(shí)現(xiàn)面包屑導(dǎo)航的示例代碼

WordPress免插件實(shí)現(xiàn)面包屑導(dǎo)航的示例代碼

2021-10-22 14:38511遇見(jiàn) Wordpress

這篇文章主要介紹了WordPress免插件實(shí)現(xiàn)面包屑導(dǎo)航,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

你如果在開(kāi)發(fā)自己的wordpress主題,想加入面包屑導(dǎo)航,而又不想使用插件的話(huà),下面的代碼對(duì)你有幫助,這里提供了網(wǎng)上較為流行的兩種代碼,一是功能非常完善的,一是一個(gè)較為簡(jiǎn)潔的代碼。

前面介紹了一種方法,你也可以嘗試一下,在文末給大家詳細(xì)介紹了自定義函數(shù)實(shí)現(xiàn)wordpress面包屑導(dǎo)航的代碼,可以點(diǎn)擊查看下。

功能非常完善代碼

1、將下面的代碼添加到主題的 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
92
/**
 * 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" >' . __( '無(wú)作為' , 'cmp' ) . '</a> ' . $delimiter . ' ';
 if ( is_category() ) { // 分類(lèi) 存檔
 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' ) { // 自定義文章類(lèi)型
 $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 ) { // 頁(yè)面
 echo $before . get_the_title() . $after;
 } elseif ( is_page() && $post->post_parent ) { // 父級(jí)頁(yè)面
 $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 頁(yè)面
 echo $before;
 _e( 'Not Found', 'cmp' );
 echo $after;
 }
 if ( get_query_var('paged') ) { // 分頁(yè)
 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>';
 }
}

前臺(tái)調(diào)用

?
1
<?php if(function_exists('cmp_breadcrumbs')) cmp_breadcrumbs();?>

下面看下自定義函數(shù)實(shí)現(xiàn)wordpress面包屑導(dǎo)航的代碼

面包屑導(dǎo)航 一是方便讀者所在的位置,更重要的是對(duì)SEO非常友好,利于蜘蛛知道你網(wǎng)站的目錄結(jié)構(gòu),所以給我們的wordpress主題添加面包屑導(dǎo)航是必須的。

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

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * WordPress 添加面包屑導(dǎo)航
 * 面包屑導(dǎo)航,直接輸出(echo)
 * Breadcrumb Trail
 * @param string $sep 導(dǎo)航對(duì)象分隔符,默認(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() .'" id="codetool">

2、前臺(tái)調(diào)用

?
1
<?php bread_nav();?>

總結(jié)

到此這篇關(guān)于WordPress免插件實(shí)現(xiàn)面包屑導(dǎo)航的示例代碼的文章就介紹到這了,更多相關(guān)WordPress面包屑導(dǎo)航內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/zcp528/article/details/108063261

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: www成人在线观看 | 日本高清视频网站www | 午夜视频在线观看91 | 一区二区国产在线 | 亚洲伊人色欲综合网 | 久久美女免费视频 | 欧美亚洲一区二区三区四区 | 色蜜桃av| 蜜桃视频在线观看免费 | 狠狠干最新网址 | 亚洲成人精品区 | 成人羞羞网站入口 | 久久国产午夜 | 中国av免费在线观看 | 久久久电影电视剧免费看 | 成人三级电影在线 | 99视频在线观看视频 | 成人午夜免费在线观看 | 免费国产不卡午夜福在线 | 99视频观看 | 成人午夜免费av | 天堂成人一区二区三区 | 成人午夜视频免费 | 性欧美xxxx极品摘花 | 中文字幕在线看第二 | 99综合视频| 日本高清com | 欧美一级高清片在线 | 在线小视频国产 | 久久久成人免费视频 | 播色网 | 久久亚洲美女视频 | 国产三级国产精品国产普男人 | 成人午夜a | 一区二区三区手机在线观看 | 亚洲无av| 亚洲免费看片网站 | 天天透天天狠天天爱综合97 | 一级看片免费视频 | 亚洲国产视频网 | 成人三级电影在线 |