博文中心

WordPress 适配 Bootstrap4 之【面包屑导航】

作者: 分类: 发布于:2019.10.23 阅读:195 次

我们先看下 Bootstrap4 默认的面包屑导航结构

<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item"><a href="#">Library</a></li>
<li class="breadcrumb-item active" aria-current="page">Data</li>
</ol>
</nav>

根据以上结构,我们构建一个函数,并放到 function.php 中。

/**
* WordPress 适配 Bootstrap4 之 面包屑导航
* http://www.inli.work/wordpress-adapter-bootstrap4-breadcrumb-navigation.html
*/
function the_crumbs( $before = '<li class="breadcrumb-item">', $delimiter = '</li><li class="breadcrumb-item">', $after = '</li>', $end_delimiter = '</li><li class="breadcrumb-item active" aria-current="page">' ) {
// $delimiter = '<span class="mx-1">/</span>'; // 分隔符
// $before = ''; // 在 当前页 前 插入
// $after = ''; // 在 当前页 后 插入
if ( !is_home() && !is_front_page() || is_paged() ) {
echo $before;
global $post;
$homeLink = home_url();
echo ' <a title="返回首页" href="' . $homeLink . '">首页</a>';
if ( is_single() && !is_page() && !is_attachment() && get_post_type() != 'post' ) { // 自定义文章类型
echo $delimiter;
$post_type = get_post_type_object( get_post_type() );
$slug = $post_type->rewrite;
echo '<a href="' . $homeLink . '/' . $slug[ 'slug' ] . '/">' . $post_type->labels->singular_name . '</a> '; //自定义文本存档 总归档 写法 2
$list = '';
foreach ( get_the_taxonomies() as $taxonomy_name => $taxonomy ) {
/*echo $taxonomy_name;*/
if ( $taxonomy_name != 'post_tag' ) {
$terms = get_the_terms( $post->ID, $taxonomy_name );
$term = $terms[ 0 ];
$parentTerm = get_term( $term->parent, $taxonomy_name );
if ( $term->parent != 0 ) {
$parentTerm_name = $parentTerm->name;
$parentTerm_link = get_term_link( $parentTerm->term_id, $taxonomy_name );
$list .= $delimiter .'<a href="' . esc_url( $parentTerm_link ) . '">' . $parentTerm_name . '</a> ' ;
}
$list .= $delimiter . '<a href="' . esc_url( get_term_link( $term->term_id, $taxonomy_name ) ) . '">' . $term->name . '</a> ' . $delimiter;
}
/* print_r ($term);*/
}
$list = substr( $list, 0, -strlen( $delimiter ) ); //移除最后一个 $delimiter
echo $list;
echo $end_delimiter . get_the_title();
} elseif ( is_single() && get_post_type() == 'post' ) { // 文章
echo $delimiter;
if ( get_option( 'page_for_posts' ) ) {//设置了文章页
echo ' <a href="' . get_post_type_archive_link( 'post' ) . '">' . get_post_type_object( 'post' )->labels->singular_name . '</a>' . $delimiter;
}
$cat = get_the_category()[ 0 ];
$cat_code = get_category_parents( $cat, TRUE, $delimiter );
$cat_code = substr( $cat_code, 0, -strlen( $delimiter ) ); //移除最后一个 $delimiter
echo $cat_code;
echo $end_delimiter . get_the_title();
}
elseif ( is_attachment() ) { // 附件
echo $delimiter;
$parent = get_post( $post->post_parent );
$cat = get_the_category( $parent->ID )[ 0 ];
$cat_code = get_category_parents( $cat, TRUE, $delimiter );
$cat_code = substr( $cat_code, 0, -strlen( $delimiter ) ); //移除最后一个 $delimiter
echo $cat_code . $delimiter;
echo '<a href="' . get_permalink( $parent ) . '">' . $parent->post_title . '</a> ';
echo $end_delimiter . '附件 - ' . get_the_title();
}
elseif ( is_page() ) { // 页面
$parents = get_post_ancestors( $post->ID );
$parents = array_reverse( $parents ); //以相反的顺序返回数组
if ( $post->post_parent ) { // 页面有父级
echo $delimiter;
$list = '';
foreach ( $parents as $crumb ) {
$list .= '<a href="' . get_permalink( $crumb ) . '">' . get_the_title( $crumb ) . '</a>' . $delimiter;
}
$list = substr( $list, 0, -strlen( $delimiter ) ); //移除最后一个 $delimiter
echo $list;
}
echo $end_delimiter . get_the_title();
}
elseif ( is_post_type_archive() ) { // 自定义文章类型 存档
echo $end_delimiter;
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$post_type_object = get_post_type_object( $post_type );
if ( !$post_type_object->has_archive ) {
echo post_type_archive_title( '', false );
} elseif ( $post_type_object->has_archive ) {
echo post_type_archive_title( '', false );
}
}
elseif ( is_category() ) { // 分类 存档
if ( get_option( 'page_for_posts' ) ) {//设置了文章页
echo $delimiter.' <a href="' . get_post_type_archive_link( 'post' ) . '">' . get_post_type_object( 'post' )->labels->singular_name . '</a>' ;
}
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category( $thisCat );
$parentCat = get_category( $thisCat->parent );
if ( $thisCat->parent != 0 ) {
echo $delimiter;
$cat_code = get_category_parents( $parentCat, TRUE, $delimiter );
$cat_code = substr( $cat_code, 0, -strlen( $delimiter ) ); //移除最后一个 $delimiter
echo $cat_code;
}
echo $end_delimiter . single_cat_title( '', false );
}
elseif ( is_tag() ) { //标签 存档
echo $end_delimiter;
printf( '关于 %s 的所有文章', single_tag_title( '', false ) );
}
elseif ( is_tax() ) { // 自定义分类 存档
echo $delimiter;
echo ' <a href="' . get_post_type_archive_link( get_post_type() ) . '">' . get_post_type_object( get_post_type() )->labels->singular_name . '</a>'; //自定义文本存档 总归档 写法 1
global $wp_query;
$tax_obj = $wp_query->get_queried_object();
$thisTax = $tax_obj->term_id;
$thisTaxonomy = $tax_obj->taxonomy;
$thisTax = get_term( $thisTax, $thisTaxonomy );
$parentTax = get_term( $thisTax->parent, $thisTaxonomy );
if ( $thisTax->parent != 0 ) {
echo $delimiter;
$tax_code = get_term_parents_list( $parentTax, $thisTaxonomy, array( 'separator' => $delimiter ) );
$tax_code = substr( $tax_code, 0, -strlen( $delimiter ) ); //移除最后一个 $delimiter
echo $tax_code;
}
echo $end_delimiter . single_term_title( '', false );
}
elseif ( is_author() && !is_post_type_archive() ) { // 作者存档
global $author;
$userdata = get_userdata( $author );
echo $end_delimiter;
printf( '来自 “%s” 的所有文章', $userdata->display_name );
}
elseif ( is_day() ) { // 天 存档
echo $delimiter;
echo '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a> ' . $delimiter;
echo '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '">' . get_the_time( 'F' ) . '</a> ';
echo $end_delimiter . get_the_time( 'd' );
}
elseif ( is_month() ) { // 月 存档
echo $delimiter;
echo '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a> ';
echo $end_delimiter . get_the_time( 'F' );
}
elseif ( is_year() ) { // 年 存档
echo $end_delimiter . get_the_time( 'Y' );
}
elseif ( is_search() ) { // 搜索结果
echo $end_delimiter;
printf( '您是在搜索 %s 吗?', get_search_query() );
}
elseif ( is_404() ) { // 404 页面
echo $end_delimiter . '亲,你迷路了!';
}
elseif ( !is_single() && !is_page() && get_post_type() != 'post' ) {
$post_type = get_post_type_object( get_post_type() );
echo $end_delimiter . $post_type->labels->singular_name;
}
// 分页
if ( ( get_query_var( 'paged' ) ) && ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) ) {
printf( ' - 第 %s 页', get_query_var( 'paged' ) );
}
echo $after;
}
}

最后在需要面包屑导航的地方,引用

<nav aria-label="breadcrumb">
<ol class="breadcrumb text-truncate">
<?php the_crumbs('<li class="breadcrumb-item">', '</li><li class="breadcrumb-item">', '</li>', '</li><li class="breadcrumb-item active" aria-current="page">'); ?>
</ol>
</nav>
2 抢沙发
我要评论