面包屑导航的作用是是告诉访问者他们目前在网站中的位置以及如何返回。同时也对SEO有着一定的影响,比如说有利于网站内链的建设,增加了网站的内部连接,提高用户体验等等。让网站内容有着明显的层次关系,WordPress 中许多知名主题均有配备,如果你仍未在网站中添加此功能,那就随 WP酷 一起折腾下吧!
示例代码
这只是我在网络中搜集的代码,可以在 WordPress 完美实现。在functions.php中加入以下代码://请放在<?php后
function get_breadcrumbs()
{
global $wp_query;
if ( !is_home() ){
echo '
- ';
echo ''. 首页 .'';
if ( is_category() )
{
$catTitle = single_cat_title( "", false );
$cat = get_cat_ID( $catTitle );
echo " » ". get_category_parents( $cat, TRUE, " » " ) ;
}
elseif ( is_archive() && !is_category() )
{
echo "» Archives";
}
elseif ( is_search() ) {
echo "» Search Results";
}
elseif ( is_404() )
{
echo "» 404 Not Found";
}
elseif ( is_single() )
{
$category = get_the_category();
$category_id = get_cat_ID( $category[0]->cat_name );
echo '» '. get_category_parents( $category_id, TRUE, " » " );
echo the_title('','', FALSE);
}
elseif ( is_page() )
{
$post = $wp_query->get_queried_object();
if ( $post->post_parent == 0 ){
echo "
» ".the_title('','', FALSE)."";} else {
$title = the_title('','', FALSE);
$ancestors = array_reverse( get_post_ancestors( $post->ID ) );
array_push($ancestors, $post->ID);
foreach ( $ancestors as $ancestor ){
if( $ancestor != end($ancestors) ){
echo '
» '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'';} else {
echo '
» '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'';}
}
}
}
echo "
";}
}
之后在你需要调用面包屑导航的任何位置加入以下代码:<?php
if (function_exists('get_breadcrumbs')){
get_breadcrumbs();
}
?>
效果参见本站文章页面顶部。