面包屑对于一个网站来说,相当于是页面结构的一个导航,是网页导航设计中一个标准设计模式,而今天我们讲的是如何通过代码来实现wordpress面包屑导航的功能,本站的老访客都知道,小编在用wordpress主题的时候十分的不喜欢用插件,尽管很方便,但会降低网站的打开速度!

教程开始了:
function get_breadcrumbs()
{
global $wp_query;
if ( !is_home() ){
// Start the UL
echo '<div class="entry clearfix breadcrumbs">';
// Add the Home link
echo '<a href="'. get_settings('home') .'">'. get_bloginfo('name') .'</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 " » 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 ' » <a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a>';
} else {
echo ' » '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'';
}
}
}
}
// End the UL
echo "</div>";
}
}
将上面的代码复制进wordpress主题文件夹下的functions.php中,然后,我们开始调用它
<?php if (function_exists('get_breadcrumbs')){get_breadcrumbs(); } ?>
将上面的调用函数放进wordpress主题文件下的archive.php、single.php、index.php、search.php等页面的相应位置,当然这是你想放哪就放哪,只要你觉得美观就好,我们都习惯放文章上方,header的下方。。。
再将这段CSS放进主题文件下的css里即可。。。
.entry {
margin-bottom: 20px;
padding: 20px;
background: #FFF;
box-shadow: 0 0 5px rgba(0,0,0,0.2);
-moz-box-shadow: 0 0 5px rgba(0,0,0,0.2);
-webkit-box-shadow: 0 0 20px rgba(0,0,0,0.2);
position: relative;
}
.breadcrumbs {
padding: 10px 20px;
font-size: 14px;
}
这样wordpress面包屑导航的功能基本就大功告成了,刷新下页面试试哈,样式效果都还不错吧!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)