下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。内存溢出小编现在分享给大家,也给大家做个参考。
面包屑导航的两个好处:
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 >'; // 在当前标签之前
$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 ><a href="' . $home_link . '">' . $text['home'] . '</a></div>';
} else {
echo '<div 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;
}
可根据自身情况进行修改和美化。
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的WordPress 添加面包屑导航(Breadcrumb)丨支持谷歌结构化数据测试全部内容,希望文章能够帮你解决WordPress 添加面包屑导航(Breadcrumb)丨支持谷歌结构化数据测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)