wordpress 不用插件实现面包屑代码一样效果,之前小编用的是插件功能还挺强大但是大部分功能都用不上觉得没有必要在说小编并不喜欢用插件尽量避免。
这里介绍如何使用纯代码实现文章导航路径分类。
1,在functions.php代码末端添加以下代码:
[code]function get_breadcrumbs()
{
global $wp_query;
if ( !is_home().get_settings(‘home’) ){
// Start the UL
echo ‘<ul>’;
// Add the Home link
echo ‘<a href=”‘. get_settings(‘home’) .'”>’. 您目前所在的位置:首页 .'</a>’;
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 “» 搜索的结果”;
}
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 “<li> » “.the_title(”,”, FALSE).”</li>”;
} 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 ‘<li> » <a href=”‘. get_permalink($ancestor) .'”>’. strip_tags( apply_filters( ‘single_post_title’, get_the_title( $ancestor ) ) ) .'</a></li>’;
} else {
echo ‘<li> » ‘. strip_tags( apply_filters( ‘single_post_title’, get_the_title( $ancestor ) ) ) .'</li>’;
}
}
}
}
// End the UL
echo “</ul>”;
}
}[/code]
2,在需要的地方调用以下代码:
[code]<div class=”breadcrumbs”>
<?php
if (function_exists(‘get_breadcrumbs’)){
get_breadcrumbs();
}
?>
</div>[/code]
3,CSS样式代码,这个可以自行修改这里我提供自己写的样式:
[code].breadcrumbs ul{
margin-left:12px;
margin-top:40px;
line-height:20px;
font-weight:bold;
word-wrap:break-word;
}
.breadcrumbs ul a{
text-decoration:none;
color:#000;
}
.breadcrumbs ul a:hover{
text-decoration:underline;
}[/code]
这里说明下functions.php所添加的代码,当访问在首页的时候是默认显示“您目前所在的位置:首页”的,如果不希望访问首页显示而是进入二级目录时才显示当前位置,你可以把以下代码。
将代码:
[code]if ( !is_home().get_settings(‘home’) )[/code]
修改为:
[code]if ( !is_home())[/code]
也就是将代码get_settings(‘home’)删除即可。