WordPress教程

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

阿里云

面包屑导航的两个好处:

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

也想出现在这里?联系我们
创客主机

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

首页预览:

文章页面:
weeiy_2014-04-27_15-23-49
谷歌结构化数据测试工具页面:
weeiy_2014-04-27_15-24-01

方法如下:

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

  1. <?php
  2. /*
  3. * WordPress 添加面包屑导航,支持谷歌结构化数据测试!
  4. *http://www.weeiy.com/wordpress-add-a-breadcrumb.HTML
  5. * 代码转载自:http://dimox.net/wordpress-breadcrumbs-without-a-plugin/
  6. */
  7. function dimox_breadcrumbs() {
  8.  
  9.  /*=微Fan建议,请保持下列设置,以免产生错误!=*/
  10.  $text['home']     = '首页'; // 文字为“主页”链接
  11.  $text['category'] = '文章目录 "%s"'; // 文本的分类页面
  12.  $text['search']   = '搜索结果 "%s" Query'; // 文本的搜索结果页面
  13.  $text['tag']      = '文章标签 "%s"'; // 文本标签页
  14.  $text['author']   = '文章发表于 %s'; // 文本的作者页面
  15.  $text['404']      = 'Error 404'; // 文本404页
  16.  
  17.  $show_current   = 1; // 1 - 显示当前文章/页/类别标题面包屑, 0 - 不显示
  18.  $show_on_home   = 0; // 1 - 在主页上显示面包屑, 0 - 不显示
  19.  $show_home_link = 1; // 1 - 显示“主页”链接, 0 - 不显示
  20.  $show_title     = 1; // 1 - 显示标题的链接, 0 - 不显示
  21.  $delimiter      = ' <small>&raquo;</small> '; // 面包屑之间的分隔符
  22.  $before         = '<span class="current">'; // 在当前标签之前
  23.  $after          = '</span>'; // 标签后面
  24.  /* === END OF OPTIONS === */
  25.  
  26.  global $post;
  27.  $home_link    = home_url('/');
  28.  $link_before  = '<span typeof="v:Breadcrumb">';
  29.  $link_after   = '</span>';
  30.  $link_attr    = ' rel="v:url" property="v:title"';
  31.  $link         = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
  32.  $parent_id    = $parent_id_2 = $post->post_parent;
  33.  $frontpage_id = get_option('page_on_front');
  34.  
  35.  if (is_home() || is_front_page()) {
  36.  
  37.  if ($show_on_home == 1) echo '<div class="breadcrumbs"><a href="' . $home_link . '">' . $text['home'] . '</a></div>';
  38.  
  39.  } else {
  40.  
  41.  echo '<div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">';
  42.  if ($show_home_link == 1) {
  43.  echo '<a href="' . $home_link . '" rel="v:url" property="v:title">' . $text['home'] . '</a>';
  44.  if ($frontpage_id == 0 || $parent_id != $frontpage_id) echo $delimiter;
  45.  }
  46.  
  47.  if ( is_category() ) {
  48.  $this_cat = get_category(get_query_var('cat'), false);
  49.  if ($this_cat->parent != 0) {
  50.  $cats = get_category_parents($this_cat->parent, TRUE, $delimiter);
  51.  if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
  52.  $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
  53.  $cats = str_replace('</a>', '</a>' . $link_after, $cats);
  54.  if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
  55.  echo $cats;
  56.  }
  57.  if ($show_current == 1) echo $before . sprintf($text['category'], single_cat_title('', false)) . $after;
  58.  
  59.  } elseif ( is_search() ) {
  60.  echo $before . sprintf($text['search'], get_search_query()) . $after;
  61.  
  62.  } elseif ( is_day() ) {
  63.  echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;
  64.  echo sprintf($link, get_month_link(get_the_time('Y'),get_the_time('m')), get_the_time('F')) . $delimiter;
  65.  echo $before . get_the_time('d') . $after;
  66.  
  67.  } elseif ( is_month() ) {
  68.  echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y')) . $delimiter;
  69.  echo $before . get_the_time('F') . $after;
  70.  
  71.  } elseif ( is_year() ) {
  72.  echo $before . get_the_time('Y') . $after;
  73.  
  74.  } elseif ( is_single() && !is_attachment() ) {
  75.  if ( get_post_type() != 'post' ) {
  76.  $post_type = get_post_type_object(get_post_type());
  77.  $slug = $post_type->rewrite;
  78.  printf($link, $home_link . '/' . $slug['slug'] . '/', $post_type->labels->singular_name);
  79.  if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;
  80.  } else {
  81.  $cat = get_the_category(); $cat = $cat[0];
  82.  $cats = get_category_parents($cat, TRUE, $delimiter);
  83.  if ($show_current == 0) $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
  84.  $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
  85.  $cats = str_replace('</a>', '</a>' . $link_after, $cats);
  86.  if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
  87.  echo $cats;
  88.  if ($show_current == 1) echo $before . get_the_title() . $after;
  89.  }
  90.  
  91.  } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
  92.  $post_type = get_post_type_object(get_post_type());
  93.  echo $before . $post_type->labels->singular_name . $after;
  94.  
  95.  } elseif ( is_attachment() ) {
  96.  $parent = get_post($parent_id);
  97.  $cat = get_the_category($parent->ID); $cat = $cat[0];
  98.  if ($cat) {
  99.  $cats = get_category_parents($cat, TRUE, $delimiter);
  100.  $cats = str_replace('<a', $link_before . '<a' . $link_attr, $cats);
  101.  $cats = str_replace('</a>', '</a>' . $link_after, $cats);
  102.  if ($show_title == 0) $cats = preg_replace('/ title="(.*?)"/', '', $cats);
  103.  echo $cats;
  104.  }
  105.  printf($link, get_permalink($parent), $parent->post_title);
  106.  if ($show_current == 1) echo $delimiter . $before . get_the_title() . $after;
  107.  
  108.  } elseif ( is_page() && !$parent_id ) {
  109.  if ($show_current == 1) echo $before . get_the_title() . $after;
  110.  
  111.  } elseif ( is_page() && $parent_id ) {
  112.  if ($parent_id != $frontpage_id) {
  113.  $breadcrumbs = array();
  114.  while ($parent_id) {
  115.  $page = get_page($parent_id);
  116.  if ($parent_id != $frontpage_id) {
  117.  $breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID));
  118.  }
  119.  $parent_id = $page->post_parent;
  120.  }
  121.  $breadcrumbs = array_reverse($breadcrumbs);
  122.  for ($i = 0; $i < count($breadcrumbs); $i++) {
  123.  echo $breadcrumbs[$i];
  124.  if ($i != count($breadcrumbs)-1) echo $delimiter;
  125.  }
  126.  }
  127.  if ($show_current == 1) {
  128.  if ($show_home_link == 1 || ($parent_id_2 != 0 && $parent_id_2 != $frontpage_id)) echo $delimiter;
  129.  echo $before . get_the_title() . $after;
  130.  }
  131.  
  132.  } elseif ( is_tag() ) {
  133.  echo $before . sprintf($text['tag'], single_tag_title('', false)) . $after;
  134.  
  135.  } elseif ( is_author() ) {
  136.  global $author;
  137.  $userdata = get_userdata($author);
  138.  echo $before . sprintf($text['author'], $userdata->display_name) . $after;
  139.  
  140.  } elseif ( is_404() ) {
  141.  echo $before . $text['404'] . $after;
  142.  
  143.  } elseif ( has_post_format() && !is_singular() ) {
  144.  echo get_post_format_string( get_post_format() );
  145.  }
  146.  
  147.  if ( get_query_var('paged') ) {
  148.  if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  149.  echo __('Page') . ' ' . get_query_var('paged');
  150.  if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  151.  }
  152.  
  153.  echo '</div><!-- .breadcrumbs -->';
  154.  
  155.  }
  156. } // end dimox_breadcrumbs()
  157. ?>

调用方法:

在你需要调用的地方插入

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

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

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

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

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

已有 676 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!