面包屑导航是帮助访问者在网站上找到自己的方式。尤其是,WordPress网站利用面包屑来帮助访问者和搜索引擎机器人理解网站的结构。 由于面包屑是优质用户体验的重要组成部分,因此搜索引擎认为它们是帮助网站在SERP中排名较高的重要排名信号。
1、什么是面包屑导航
面包屑导航允许用户跟踪从首页到他们访问的最后一页的路径。 因此,很容易想象面包屑在网站上提供了通往特定目的地的途径。如果使用得当,面包屑导航可以在您的网页上添加上下文,同时减少许多可能负面影响SEO相关网站性能数字的因素,包括跳出率,退出率和平均会话次数。
2、为什么面包屑导航重要
在帮助用户了解WordPress网站的组织方式时,面包屑导航起着至关重要的作用。 它们促进了积极的行动,包括促使用户访问网站结构中深处的页面,否则这些页面可能不会收到访问量。
同时面包屑导航可帮助用户了解网站的布局,从而使他们能够轻松导航至更高级别的页面,以便“返回”高级类别页面以及整个首页。此外,面包屑导航允许用户以更快的速度浏览WordPress网站并查看内容。 由于使用非常简单,因此所有用户,无论其技术水平如何,都可以轻松浏览该网站。
3、如何无插件添加面包屑导航
添加的功能很简单,您只需要把下面的代码放在您的function.php文件末尾:
function getcategory_with_child($category){
$cats = array();
foreach ($category as $cat){
if ($cat->category_parent!=0){
array_push($cats, $cat);
}
}
return $cats;
}
function get_category_parents_exe( $id, $link = false, $nicename = false, $visited = array() ) {
$chains = '';
$term = get_queried_object();
$parent = get_term( $id, $term->taxonomy );
if ( is_wp_error( $parent ) ){
return $parent;
}
if ( $nicename ){
$name = $parent->slug;
}
else{
$name = $parent->name;
}
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chains .= get_category_parents_exe( $parent->parent, $link, $nicename, $visited );
}
if ( $link ){
$chains .= $name .'__/__'. get_category_link( $parent->term_id ).',';
}
else{
$chains .= $name .'__/__,';
}
return $chains;
}
function wp_breadcrumbs(){
global $post;
$breadcrumbs = array();
if ( !is_front_page() ) {
$breadcrumbs[] = array(
'title' => 'Home',
'link' => home_url()
);
if ( is_home() ) {
$breadcrumbs[] = array(
'title' => __('Blog'),
'link' => null
);
}
if ( is_archive() ) {
$post_type = get_post_type();
$post_type_object = get_post_type_object($post_type);
$post_type_archive = get_post_type_archive_link($post_type);
$breadcrumbs[] = array(
'title' => $post_type_object->labels->name,
'link' => $post_type_archive
);
if(is_category() || is_tax()){
$term = get_queried_object();
$get_term_parents = get_category_parents_exe($term->term_id, true);
$get_term_parents = rtrim($get_term_parents, ',');
$term_parents = explode(',', $get_term_parents);
$terms_count = count($term_parents);
foreach($term_parents as $key => $parents) {
$parents = explode('__/__', $parents);
if ($key < $terms_count-1){
$breadcrumbs[] = array(
'title' => $parents[0],
'link' => $parents[1]
);
} else {
$breadcrumbs[] = array(
'title' => $parents[0],
'link' => null
);
}
}
}
if ( is_tag() ) {
$breadcrumbs[] = array(
'title' => single_tag_title('', false),
'link' => null
);
}
if ( is_author() ) {
global $author;
$userdata = get_userdata( $author );
$breadcrumbs[] = array(
'title' => __('Author: ') . $userdata->display_name,
'link' => null
);
}
if( is_day() ) {
$breadcrumbs[] = array(
'title' => get_the_time('Y'),
'link' => get_year_link( get_the_time('Y') )
);
$breadcrumbs[] = array(
'title' => get_the_time('M'),
'link' => get_month_link( get_the_time('Y'), get_the_time('m') )
);
$breadcrumbs[] = array(
'title' => get_the_time('jS') . ' ' . get_the_time('M'),
'link' => null
);
}
if( is_month() ) {
$breadcrumbs[] = array(
'title' => get_the_time('Y'),
'link' => get_year_link( get_the_time('Y') )
);
$breadcrumbs[] = array(
'title' => get_the_time('M'),
'link' => null
);
}
if( is_year() ) {
$breadcrumbs[] = array(
'title' => get_the_time('Y'),
'link' => null
);
}
}
if ( get_query_var('paged') ) {
if ( ! is_archive() ) {
$breadcrumbs[] = array(
'title' => get_post_type_object(get_post_type())->labels->singular_name,
'link' => get_post_type_archive_link(get_post_type_object(get_post_type())->query_var)
);
}
$breadcrumbs[] = array(
'title' => __('Page ') . get_query_var('paged'),
'link' => null
);
}
if ( is_search() ) {
$breadcrumbs[] = array(
'title' => __('Search results for ') . get_search_query(),
'link' => null
);
}
if ( is_404() ) {
$breadcrumbs[] = array(
'title' => __('Error 404'),
'link' => null
);
}
if(is_singular()){
if(is_single()){
$post_type = get_post_type();
$post_type_object = get_post_type_object($post_type);
$post_type_archive = get_post_type_archive_link($post_type);
$breadcrumbs[] = array(
'title' => $post_type_object->labels->name,
'link' => $post_type_archive
);
$term = get_the_category();
if(!empty($term)) {
$term_wc = getcategory_with_child($term);
if(!empty($term_wc)){
$term = $term_wc;
} else {
$term = $term;
}
if(!empty($term)){
$term = $term[count($term)-1];
$get_term_parents = get_category_parents_exe($term->term_id, true);
$get_term_parents = rtrim($get_term_parents, ',');
$term_parents = explode(',', $get_term_parents);
foreach($term_parents as $parents) {
$parents = explode('__/__', $parents);
$breadcrumbs[] = array(
'title' => $parents[0],
'link' => $parents[1]
);
}
}
}
$taxonomy = 'product_cat';
$taxonomy_exists = taxonomy_exists($taxonomy);
if(empty($term) && $taxonomy_exists) {
$taxonomy_terms = get_the_terms( $post->ID, $taxonomy );
$base_tex = $taxonomy_terms[0];
if(count($taxonomy_terms) > 1){
$term = get_term( $taxonomy_terms[0]->parent, $taxonomy );
$get_term_parents = get_category_parents_exe($term->term_id, true);
$get_term_parents = rtrim($get_term_parents, ',');
$term_parents = explode(',', $get_term_parents);
foreach($term_parents as $parents) {
$parents = explode('__/__', $parents);
$breadcrumbs[] = array(
'title' => $parents[0],
'link' => $parents[1]
);
}
}
$breadcrumbs[] = array(
'title' => $base_tex->name,
'link' => get_term_link( $base_tex->term_id )
);
}
$breadcrumbs[] = array(
'title' => get_the_title(),
'link' => null
);
}
if(is_page()){
if( $post->post_parent ){
$anc = get_post_ancestors( $post->ID );
$anc = array_reverse($anc);
if ( !isset( $parents ) ) $parents = null;
foreach ( $anc as $ancestor ) {
$breadcrumbs[] = array(
'title' => get_the_title($ancestor),
'link' => get_permalink($ancestor)
);
}
$breadcrumbs[] = array(
'title' => get_the_title(),
'link' => null
);
} else {
$breadcrumbs[] = array(
'title' => get_the_title(),
'link' => null
);
}
}
}
}
$output = '';
$output .= '<ol class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">';
$breadcrumbs = array_combine(range(1, count($breadcrumbs)), $breadcrumbs);
foreach ($breadcrumbs as $key => $breadcrumb) {
$output .= '<li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">';
if(!empty($breadcrumb['link'])){
$output .= '<a itemprop="item" href="'.$breadcrumb['link'].'">';
}
$output .= '<span itemprop="name">'.$breadcrumb['title'].'</span>';
if(!empty($breadcrumb['link'])){
$output .= '</a>';
}
$output .= '<meta itemprop="position" content="'.$key.'" />';
$output .= '</li> ';
}
$output .= '</ol>';
echo $output;
}
然后在您需要添加面包屑导航的位置。比如文章页,归类页面,自定义文章页面等添加下面的代码,就可以使用面包屑导航了
<?php
if(function_exists('wp_breadcrumbs')){
wp_breadcrumbs();
}
?>
本文已在Ie主题由99839发布
文章来源:https://ietheme.com/how-to-add-breadcrumbs-wordpress.html