### [分享一个WordPress面包屑导航代码](https://www.prosaas.cn/article/8282)
**Published:** 2021-05-06T02:13:33
**Author:** 5201314
**Excerpt:** 下面由WordPress教程栏目给大家分享一个WordPress面包屑导航代码,希望对需要的朋友有所帮助!转载…
下面由**WordPress**教程栏目给大家分享一个WordPress面包屑导航代码,希望对需要的朋友有所帮助!
转载分享一段WordPress面包屑导航代码,支持自定义帖子类型、自定义分类,但貌似在分类归档页面不能显示父子分类层级有点遗憾。
将代码添加到当前主题函数模板functions.php中:
/\*\*
\* WordPress Breadcrumbs
\*/
function tsh\_wp\_custom\_breadcrumbs() {
$separator = '/';
$breadcrumbs\_id = 'tsh\_breadcrumbs';
$breadcrumbs\_class = 'tsh\_breadcrumbs';
$home\_title = esc\_html\_\_('Home', 'your-domain');
// Add here you custom post taxonomies
$tsh\_custom\_taxonomy = 'product\_cat';
global $post,$wp\_query;
// Hide from front page
if ( !is\_front\_page() ) {
echo '
';
// Home
echo '- ' . $home\_title . '
';
echo '- ' . $separator . '
';
if ( is\_archive() && !is\_tax() && !is\_category() && !is\_tag() ) {
echo '- ' . post\_type\_archive\_title('', false) . '
';
} else if ( is\_archive() && is\_tax() && !is\_category() && !is\_tag() ) {
// For Custom post type
$post\_type = get\_post\_type();
// Custom post type name and link
if($post\_type != 'post') {
$post\_type\_object = get\_post\_type\_object($post\_type);
$post\_type\_archive = get\_post\_type\_archive\_link($post\_type);
echo '- ' . $post\_type\_object->labels->name . '
';
echo '- ' . $separator . '
';
}
$custom\_tax\_name = get\_queried\_object()->name;
echo '- ' . $custom\_tax\_name . '
';
} else if ( is\_single() ) {
$post\_type = get\_post\_type();
if($post\_type != 'post') {
$post\_type\_object = get\_post\_type\_object($post\_type);
$post\_type\_archive = get\_post\_type\_archive\_link($post\_type);
echo '- ' . $post\_type\_object->labels->name . '
';
echo '- ' . $separator . '
';
}
// Get post category
$category = get\_the\_category();
if(!empty($category)) {
// Last category post is in
$last\_category = $category\[count($category) - 1\];
// Parent any categories and create array
$get\_cat\_parents = rtrim(get\_category\_parents($last\_category->term\_id, true, ','),',');
$cat\_parents = explode(',',$get\_cat\_parents);
// Loop through parent categories and store in variable $cat\_display
$cat\_display = '';
foreach($cat\_parents as $parents) {
$cat\_display .= '- '.$parents.'
';
$cat\_display .= '- ' . $separator . '
';
}
}
$taxonomy\_exists = taxonomy\_exists($tsh\_custom\_taxonomy);
if(empty($last\_category) && !empty($tsh\_custom\_taxonomy) && $taxonomy\_exists) {
$taxonomy\_terms = get\_the\_terms( $post->ID, $tsh\_custom\_taxonomy );
$cat\_id = $taxonomy\_terms\[0\]->term\_id;
$cat\_nicename = $taxonomy\_terms\[0\]->slug;
$cat\_link = get\_term\_link($taxonomy\_terms\[0\]->term\_id, $tsh\_custom\_taxonomy);
$cat\_name = $taxonomy\_terms\[0\]->name;
}
// If the post is in a category
if(!empty($last\_category)) {
echo $cat\_display;
echo '- ' . get\_the\_title() . '
';
// Post is in a custom taxonomy
} else if(!empty($cat\_id)) {
echo '- ' . $cat\_name . '
';
echo '- ' . $separator . '
';
echo '- ' . get\_the\_title() . '
';
} else {
echo '- ' . get\_the\_title() . '
';
}
} else if ( is\_category() ) {
// Category page
echo '- ' . single\_cat\_title('', false) . '
';
} else if ( is\_page() ) {
// Standard page
if( $post->post\_parent ){
// Get parents
$anc = get\_post\_ancestors( $post->ID );
// Get parents order
$anc = array\_reverse($anc);
// Parent pages
if ( !isset( $parents ) ) $parents = null;
foreach ( $anc as $ancestor ) {
$parents .= '- ' . get\_the\_title($ancestor) . '
';
$parents .= '- ' . $separator . '
';
}
// Render parent pages
echo $parents;
// Active page
echo '- ' . get\_the\_title() . '
';
} else {
// Just display active page if not parents pages
echo '- ' . get\_the\_title() . '
';
}
} else if ( is\_tag() ) { // Tag page
// Tag information
$term\_id = get\_query\_var('tag\_id');
$taxonomy = 'post\_tag';
$args = 'include=' . $term\_id;
$terms = get\_terms( $taxonomy, $args );
$get\_term\_id = $terms\[0\]->term\_id;
$get\_term\_slug = $terms\[0\]->slug;
$get\_term\_name = $terms\[0\]->name;
// Return tag name
echo '- ' . $get\_term\_name . '
';
} elseif ( is\_day() ) { // Day archive page
// Year link
echo '- ' . get\_the\_time('Y') . ' Archives
';
echo '- ' . $separator . '
';
// Month link
echo '- ' . get\_the\_time('M') . ' Archives
';
echo '- ' . $separator . '
';
// Day display
echo '- ' . get\_the\_time('jS') . ' ' . get\_the\_time('M') . ' Archives
';
} else if ( is\_month() ) { // Month Archive
// Year link
echo '- ' . get\_the\_time('Y') . ' Archives
';
echo '- ' . $separator . '
';
// Month display
echo '- ' . get\_the\_time('M') . ' Archives
';
} else if ( is\_year() ) { // Display year archive
echo '- ' . get\_the\_time('Y') . ' Archives
';
} else if ( is\_author() ) { // Author archive
// Get the author information
global $author;
$userdata = get\_userdata( $author );
// Display author name
echo '- ' . 'Author: ' . $userdata->display\_name . '
';
} else if ( get\_query\_var('paged') ) {
// Paginated archives
echo '- '.\_\_('Page') . ' ' . get\_query\_var('paged') . '
';
} else if ( is\_search() ) {
// Search results page
echo '- Search results for: ' . get\_search\_query() . '
';
} elseif ( is\_404() ) {
// 404 page
echo '- ' . 'Error 404' . '
';
}
echo '
';
}
}
将调用代码放到主题模板适当位置比如header.php中:
配套样式:
#tsh\_breadcrumbs .separator{
font-size:20px;
color:#ccc;
font-weight:100;
}
#tsh\_breadcrumbs{
overflow:hidden;
text-align: center;
list-style:none;
margin:11px 0;
}
#tsh\_breadcrumbs li{
margin-right:14px;
display:inline-block;
vertical-align:middle;
}
以上就是分享一个WordPress面包屑导航代码的详细内容,更多请关注知企PROSAAS其它相关文章!
**Tags:** php, WordPress, 函数模板, 子类
**Categories:** WordPress
---