WordPress 添加面包屑导航(Breadcrumb)丨支持谷歌结构化数据测试

面包屑导航的两个好处:

1、使用户明白自己在哪个位置,以免遗失在某个角落。

创客主机
2、为了谷歌结构化,让体验更友好,结构清晰。

首页预览:

文章页面:

谷歌结构化数据测试工具页面:

 

方法如下:

将以下代码插入 functions.php 文件的 ?> 后面。

<?php
/*
* wordpress 添加面包屑导航,支持谷歌结构化数据测试!
*http://www.weeiy.com/wordpress-add-a-breadcrumb.html
* 代码转载自:http://dimox.net/wordpress-breadcrumbs-without-a-plugin/
*/
function dimox_breadcrumbs() {
 
 /*=微Fan建议,请保持下列设置,以免产生错误!=*/
 $text['home']     = '首页'; // 文字为“主页”链接
 $text['category'] = '文章目录 "%s"'; // 文本的分类页面
 $text['search']   = '搜索结果 "%s" Query'; // 文本的搜索结果页面
 $text['tag']      = '文章标签 "%s"'; // 文本标签页
 $text['author']   = '文章发表于 %s'; // 文本的作者页面
 $text['404']      = 'Error 404'; // 文本404页
 
 $show_current   = 1; // 1 - 显示当前文章/页/类别标题面包屑, 0 - 不显示
 $show_on_home   = 0; // 1 - 在主页上显示面包屑, 0 - 不显示
 $show_home_link = 1; // 1 - 显示“主页”链接, 0 - 不显示
 $show_title     = 1; // 1 - 显示标题的链接, 0 - 不显示
 $delimiter      = ' <small>&raquo;</small> '; // 面包屑之间的分隔符
 $before         = '<span class="current">'; // 在当前标签之前
 $after          = '</span>'; // 标签后面
 /* === END OF OPTIONS === */
 
 global $post;
 $home_link    = home_url('/');
 $link_before  = '<span typeof="v:Breadcrumb">';
 $link_after   = '</span>';
 $link_attr    = ' rel="v:url" property="v:title"';
 $link         = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
 $parent_id    = $parent_id_2 = $post->post_parent;
 $frontpage_id = get_option('page_on_front');
 
 if (is_home() || is_front_page()) {
 
 if ($show_on_home == 1) echo '<div class="breadcrumbs"><a href="' . $home_link . '">' . $text['home'] . '</a></div>';
 
 } else {
 
 echo '<div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">';
 if ($show_home_link == 1) {
 echo '<a href="' . $home_link . '" rel="v:url" property="v:title">' . $text['home'] . '</a>';
 if ($frontpage_id == 0 || $parent_id != $frontpage_id) echo $delimiter;
 }
 
 if ( is_category() ) {
 $this_cat = get_category(get_query_var('cat'), false);
 if ($this_cat->parent != 0) {
 $cats = get_category_parents($this_cat->parent, TRUE, $delimiter);
 if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
 $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
 $cats = str_replace('</a>', '</a>' . $link_after, $cats);
 if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
 echo $cats;
 }
 if ($show_current == 1) echo $before . sprintf($text['category'], single_cat_title('', false)) . $after;
 
 } elseif ( is_search() ) {
 echo $before . sprintf($text['search'], get_search_query()) . $after;
 
 } elseif ( is_day() ) {
 echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;
 echo sprintf($link, get_month_link(get_the_time('Y'),get_the_time('m')), get_the_time('F')) . $delimiter;
 echo $before . get_the_time('d') . $after;
 
 } elseif ( is_month() ) {
 echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $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;
 printf($link, $home_link . '/' . $slug['slug'] . '/', $post_type->labels->singular_name);
 if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;
 } else {
 $cat = get_the_category(); $cat = $cat[0];
 $cats = get_category_parents($cat, TRUE, $delimiter);
 if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
 $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
 $cats = str_replace('</a>', '</a>' . $link_after, $cats);
 if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
 echo $cats;
 if ($show_current == 1) echo $before . get_the_title() . $after;
 }
 
 } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
 $post_type = get_post_type_object(get_post_type());
 echo $before . $post_type->labels->singular_name . $after;
 
 } elseif ( is_attachment() ) {
 $parent = get_post($parent_id);
 $cat = get_the_category($parent->ID); $cat = $cat[0];
 if ($cat) {
 $cats = get_category_parents($cat, TRUE, $delimiter);
 $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
 $cats = str_replace('</a>', '</a>' . $link_after, $cats);
 if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
 echo $cats;
 }
 printf($link, get_permalink($parent), $parent->post_title);
 if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;
 
 } elseif ( is_page() && !$parent_id ) {
 if ($show_current == 1) echo $before . get_the_title() . $after;
 
 } elseif ( is_page() && $parent_id ) {
 if ($parent_id != $frontpage_id) {
 $breadcrumbs = array();
 while ($parent_id) {
 $page = get_page($parent_id);
 if ($parent_id != $frontpage_id) {
 $breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID));
 }
 $parent_id = $page->post_parent;
 }
 $breadcrumbs = array_reverse($breadcrumbs);
 for ($i = 0; $i < count($breadcrumbs); $i++) {
 echo $breadcrumbs[$i];
 if ($i != count($breadcrumbs)-1) echo $delimiter;
 }
 }
 if ($show_current == 1) {
 if ($show_home_link == 1 || ($parent_id_2 != 0 && $parent_id_2 != $frontpage_id)) echo $delimiter;
 echo $before . get_the_title() . $after;
 }
 
 } elseif ( is_tag() ) {
 echo $before . sprintf($text['tag'], single_tag_title('', false)) . $after;
 
 } elseif ( is_author() ) {
 global $author;
 $userdata = get_userdata($author);
 echo $before . sprintf($text['author'], $userdata->display_name) . $after;
 
 } elseif ( is_404() ) {
 echo $before . $text['404'] . $after;
 
 } elseif ( has_post_format() && !is_singular() ) {
 echo get_post_format_string( get_post_format() );
 }
 
 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 ')';
 }
 
 echo '</div><!-- .breadcrumbs -->';
 
 }
} // end dimox_breadcrumbs()
?>

调用方法:

在你需要调用的地方插入

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

另外需要在样式表中添加 CSS 样式,不然会影响版面,例如:

.breadcrumbs {
margin: 0 0 5px;
padding: 9px 20px 7px;
background-color: #f7f7f7;
}

可根据自身情况进行修改和美化。

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » WordPress 添加面包屑导航(Breadcrumb)丨支持谷歌结构化数据测试