2015年4月1日 / 148次阅读
WordPress
麦新杰在之前的分享中,已经说明了什么是面包屑导航,以及在同一篇文章存在多路径的情况下,如何实现面包屑导航的问题。在云上小悟改版前,麦新杰放弃了面包屑导航,现在网站改版,面包屑导航回归,不过,这次回归的是升级版的面包屑导航。
面包屑导航升级的地方在哪里呢?
这一次升级,对于末端的内容增加了链接,比如如果是文章页,文章标题就带有链接,如果是分类页,分类名下面就带有链接,如果是作者页,作者显示名称下面也有链接。
下图为例,注意红框红圈,这是麦新杰实现面包屑导航的亮点。
云上小悟升级版的面包屑导航示意图
亮点:
1, 对文章的多路径支持;(即一篇文章同属于多个分类的情况)
2, 增加末端链接,方便页面刷新。
升级版的面包屑导航实现代码
/* get the category for bread nav */
function mt_get_the_cat($post_id) {
$categories = wp_get_post_terms( $post_id, 'category', array('orderby' => 'term_group') );
$output = '';
$temp = 0;
if($categories){
foreach($categories as $category) {
if($temp > 0) {
$output .= ' | ';
}
$output .= '<a target="_blank" href="'. get_category_link( $category->term_id ) . '">' . $category->name . '</a>';
$temp++;
}
}
return $output;
}
/* 面包屑导航栏生成 */
function mt_bread_nav() {
$rc = '<p style="font-size:12px;margin:0;">当前位置 : ';
$rc .= '<a target="_blank" href="https://www.maixj.net">首页</a> » ';
if (is_front_page()) {
/* do nothing. */
}
if (is_single() || is_page()) {
$post_id = get_the_ID();
/* get all cat */
$rc .= mt_get_the_cat($post_id);
$rc .= ' » ';
$rc .= '<a href="'. get_permalink() . '">' . get_the_title($post_id) . '</a> 正文';
}
if (is_category()) {
// Get the ID of a given category
$category_id = get_cat_ID( get_category(get_query_var('cat'))->name );
// Get the URL of this category
$category_link = get_category_link( $category_id );
$rc .= '栏目 <a target="_blank" href="' . $category_link . '">' . get_category(get_query_var('cat'))->name . '</a> 的文章列表';
}
if (is_tag()) {
$tag_id = get_query_var('tag_id');
$rc .= '标签 <a href="' . get_tag_link($tag_id) . '">' . single_tag_title("", false) . '</a> 的文章列表';
}
if(is_date()){
$rc .= '文章归档 <a href="'. get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_date('Y年n月') . '</a> 的文章列表';
}
if(is_author()){
$rc .= '小伙伴 <a href="' . get_author_posts_url( get_the_author_meta( 'ID' )) . '">' . get_the_author_meta( 'display_name' ) . '</a> 的文章列表';
}
if(is_search())
{
$rc .= '搜索 <a href="https://www.maixj.net/?s=' . trim(get_search_query()) . '">' . trim(get_search_query()) . '</a> 的文章列表(每页显示最多49篇文章)';
}
$rc .= '</p>';
return $rc;
}
以上代码是云上小悟现在正在使用的面包屑导航代码,请自行调整参数后使用。
本文链接:https://www.maixj.net/wz/shengjiban-mianbaoxiedaohagn-4383
©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1。云上小悟网站部分内容来源于网络,转载目的是为了整合信息,收藏学习,服务大家,有些转载内容也难以判断是否有侵权问题,如果侵犯了您的权益,请及时联系站长,我会立即删除。