當(dāng)前位置:首頁>WordPress建站>WordPress開發(fā)>WordPress 添加面包屑導(dǎo)航 Breadcrumb

WordPress 添加面包屑導(dǎo)航 Breadcrumb

幾天前,@斌果 讓我分享一下 WordPress 添加面包屑導(dǎo)航的代碼,今天就拿出自己現(xiàn)在用的代碼,來自老外的某個主題。關(guān)于什么是面包屑導(dǎo)航,以及它的作用,倡萌就不闡述了,直接看下圖就明白了:

breadcrumb-wpdaxue_com

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

/**
 * WordPress 添加面包屑導(dǎo)航 
 * http://www.ydqwiac.cn/wordpress-add-a-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">'.__( 'You are here:' , 'cmp' );
		global $post;
		$homeLink = home_url();
		echo ' <a itemprop="breadcrumb" href="' . $homeLink . '">' . __( 'Home' , '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')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
			echo '<a itemprop="breadcrumb"  href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . 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')) . '">' . 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'] . '/">' . $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) . '">' . $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) . '">' . 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>';
	}
}

以上的代碼功能已經(jīng)十分完善了,帶有 Html5微數(shù)據(jù),包含本地化翻譯(請將所有 ‘cmp’ 修改為你的主題專用的 textdomain,不明白的請閱讀:讓W(xué)ordPress主題支持語言本地化

在主題模板中使用以下代碼調(diào)用:

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

至于 css美化什么的,自己折騰。

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號 WPDAXUE
WordPress開發(fā)

WordPress 添加額外選項字段到常規(guī)設(shè)置頁面

2013-10-9 8:45:38

WordPress開發(fā)

WordPress 使用 gettext 鉤子替換本地化翻譯文本

2013-10-21 7:49:00

22 條回復(fù) A文章作者 M管理員
  1. 牧澤

    文中給出的代碼很棒,但對于新手開發(fā)者來說可能太復(fù)雜了,我找到了一個稍微簡單點的版本,希望能夠幫助到大家
    https://www.npc.ink/5015.html

  2. stream

    elseif ( is_single() && !is_attachment() ) { // 文章
    if ( get_post_type() != ‘post’ ) { // 自定義文章類型
    $post_type = get_post_type_object(get_post_type());
    $slug = $post_type->rewrite;
    echo ” . $post_type->labels->singular_name . ‘ ‘ . $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;
    }
    這一段有點問題,如果我用的是‘’taxonomy-分類法.php‘’模版,我注冊的的post type從這個頁面進入的文章頁面的文章的面包屑導(dǎo)航鏈接是錯誤的。

  3. 請問如何把面包屑的文章名,改為正文呢,就像你的面包屑一樣如:

    首頁-WordPress開發(fā)-正文 [就是這里,我的是顯示文章名,如何改為正文呢]

  4. 支持自定義文章類型嗎

    • 我試了,不支持自定義文章類型,請博主指教

  5. 是否支持ie8瀏覽器呢?

?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

大英县| SHOW| 定日县| 水城县| 垣曲县| 岳普湖县| 达日县| 淮阳县| 白山市| 广饶县| 长宁区| 乐平市| 浏阳市| 无棣县| 连江县| 镇江市| 巴林左旗| 绍兴县| 湛江市| 宜昌市| 确山县| 农安县| 墨江| 晋中市| 固安县| 浑源县| 博客| 城固县| 兴宁市| 无为县| 平山县| 尖扎县| 桃江县| 咸丰县| 九江市| 安多县| 景德镇市| 望都县| 荥经县| 乐平市| 邻水|