WordPress 添加面包屑导航(Breadcrumb)丨支持谷歌结构化数据测试
以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考 面包屑导航的两个好处: 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>»</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_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,$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,$delimiter); $cats = str_replace('<a',$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; } 可根据自身情况进行修改和美化。 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |