面包屑导航是一系列连接的导航链接,这些链接显示了您浏览网站页面的路径,面包屑通常显示在页面顶部,以方便访问。主要目的是使用户能够轻松地回溯,从而改善站点的UX,除此以外,面包屑导航对于整体网站体验和SEO都有好处。那么WordPress如何添加面包屑导航?

我们可以通过自定义代码实现面包屑,将以下代码添加到现用主题的functions.php文件中:
// WordPress Breadcrumb Function// Add this code into your theme function file.function ah_breadcrumb() {// Check if is front/home page, returnif ( is_front_page() ) {return;}// Defineglobal $post;$custom_taxonomy = ''; // If you have custom taxonomy place it here$defaults = array('seperator' => '»','id' => 'ah-breadcrumb','classes' => 'ah-breadcrumb','home_title' => esc_html__( 'Home', '' ));$sep = '
- ‘
. esc_html( $defaults['seperator'] ) .'
‘;
// Start the breadcrumb with a link to your homepageecho '
- .
esc_attr( $defaults['id'] ) .'" class="'. esc_attr( $defaults['classes'] ) .'">';// Creating home linkecho '
- .
get_home_url() .'">'. esc_html( $defaults['home_title'] ) .'
‘ . $sep;
if ( is_single() ) {// Get posts type$post_type = get_post_type();// If post type is not postif( $post_type != 'post' ) {$post_type_object = get_post_type_object( $post_type );$post_type_link = get_post_type_archive_link( $post_type );echo '
- .
$post_type_link .'">'. $post_type_object->labels->name .'
‘. $sep;
}// Get categories$category = get_the_category( $post->ID );// If category not emptyif( !empty( $category ) ) {// Arrange category parent to child$category_values = array_values( $category );$get_last_category = end( $category_values );// $get_last_category = $category[count($category) - 1];$get_parent_category = rtrim( get_category_parents( $get_last_category->term_id, true, ',' ), ',' );$cat_parent = explode( ',', $get_parent_category );// Store category in $display_category$display_category = '';foreach( $cat_parent as $p ) {$display_category .= '
- ‘
. $p .'
‘ . $sep;
}}// If it's a custom post type within a custom taxonomy$taxonomy_exists = taxonomy_exists( $custom_taxonomy );if( empty( $get_last_category ) && !empty( $custom_taxonomy ) && $taxonomy_exists ) {$taxonomy_terms = get_the_terms( $post->ID, $custom_taxonomy );$cat_id = $taxonomy_terms[0]->term_id;$cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy);$cat_name = $taxonomy_terms[0]->name;}// Check if the post is in a categoryif( !empty( $get_last_category ) ) {echo $display_category;echo '
- ‘
. get_the_title() .'
‘;
} else if( !empty( $cat_id ) ) {echo '
- .
$cat_link .'">'. $cat_name .'
‘ . $sep;
echo '
- ‘
. get_the_title() .'
‘;
} else {echo '
- ‘
. get_the_title() .'
‘;
}} else if( is_archive() ) {if( is_tax() ) {// Get posts type$post_type = get_post_type();// If post type is not postif( $post_type != 'post' ) {$post_type_object = get_post_type_object( $post_type );$post_type_link = get_post_type_archive_link( $post_type );echo '
- .
$post_type . '">. $post_type_link . '">' . $post_type_object->labels->name . '
‘ . $sep;
}$custom_tax_name = get_queried_object()->name;echo '
- ‘
. $custom_tax_name .'
‘;
} else if ( is_category() ) {$parent = get_queried_object()->category_parent;if ( $parent !== 0 ) {$parent_category = get_category( $parent );$category_link = get_category_link( $parent );echo '
- .
esc_url( $category_link ) .'">'. $parent_category->name .'
‘ . $sep;
}echo '
- ‘
. single_cat_title( '', false ) .'
‘;
} else if ( is_tag() ) {// Get tag information$term_id = get_query_var('tag_id');$taxonomy = 'post_tag';$args = 'include=' . $term_id;$terms = get_terms( $taxonomy, $args );$get_term_name = $terms[0]->name;// Display the tag nameecho '
- ‘
. $get_term_name .'
‘;
} else if( is_day() ) {// Day archive// Year linkecho '
- .
get_year_link( get_the_time('Y') ) .'">'. get_the_time('Y') . ' Archives
‘ . $sep;
// Month linkecho '
- .
get_month_link( get_the_time('Y'), get_the_time('m') ) .'">'. get_the_time('M') .' Archives
‘ . $sep;
// Day displayecho '
- ‘
. get_the_time('jS') .' '. get_the_time('M'). ' Archives
‘;
} else if( is_month() ) {// Month archive// Year linkecho '
- .
get_year_link( get_the_time('Y') ) .'">'. get_the_time('Y') . ' Archives
‘ . $sep;
// Month Displayecho '
- ‘
. get_the_time('M') .' Archives
‘;
} else if ( is_year() ) {// Year Displayecho '
- ‘
. get_the_time('Y') .' Archives
‘;
} else if ( is_author() ) {// Auhor archive// Get the author informationglobal $author;$userdata = get_userdata( $author );// Display author nameecho '
- ‘
. 'Author: '. $userdata->display_name . '
‘;
} else {echo '
- ‘
. post_type_archive_title() .'
‘;
}} else if ( is_page() ) {// Standard pageif( $post->post_parent ) {// If child page, get parents$anc = get_post_ancestors( $post->ID );// Get parents in the right order$anc = array_reverse( $anc );// Parent page loopif ( !isset( $parents ) ) $parents = null;foreach ( $anc as $ancestor ) {$parents .= '
- .
get_permalink( $ancestor ) .'">'. get_the_title( $ancestor ) .'
‘ . $sep;
}// Display parent pagesecho $parents;// Current pageecho '
- ‘
. get_the_title() .'
‘;
} else {// Just display current page if not parentsecho '
- ‘
. get_the_title() .'
‘;
}} else if ( is_search() ) {// Search results pageecho '
- Search results for: ‘
. get_search_query() .'
‘;
} else if ( is_404() ) {// 404 pageecho '
- ‘
. 'Error 404' . '
‘;
}// End breadcrumbecho '';}
然后还需要将以下行添加到主题的header.php文件中:
php// Call the breadcrumb function where you want to displayif ( function_exists('ah_breadcrumb') ) ah_breadcrumb();?>
第一个片段将面包屑添加到您的主题。第二个“调用”相关功能,以便导航链接出现在页眉中。请注意,您可能需要删除开头的<?php ,此代码才能与主题的现有文件一起使用。


