方法简介
面包屑对于一个网站来说,相当于是页面结构的一个导航,是网页导航设计中一个标准设计模式,而今天我们讲的是如何通过代码来实现wordpress面包屑导航的功能!
方法步骤
将代码复制进wordpress主题文件夹下的functions.php中
function get_breadcrumbs() { global $wp_query; if ( !is_home() ){ // Start the UL echo '<ul class="breadcrumbs">'; // Add the Home link echo '<li><a href="'. get_settings('home') .'">'. get_bloginfo('name') .'</a></li>'; if ( is_category() ) { $catTitle = single_cat_title( "", false ); $cat = get_cat_ID( $catTitle ); echo "<li> » ". get_category_parents( $cat, TRUE, " » " ) ."</li>"; } elseif ( is_archive() && !is_category() ) { echo "<li> » Archives</li>"; } elseif ( is_search() ) { echo "<li> » Search Results</li>"; } elseif ( is_404() ) { echo "<li> » 404 Not Found</li>"; } elseif ( is_single() ) { $category = get_the_category(); $category_id = get_cat_ID( $category[0]->cat_name ); echo '<li> » '. get_category_parents( $category_id, TRUE, " » " ); echo the_title('','', FALSE) ."</li>"; } 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>"; } }
开始调用
<?php if (function_exists('get_breadcrumbs')){get_breadcrumbs(); } ?>
将上面的调用函数放进wordpress主题文件下的archive.php、single.php、index.php、search.php等页面的相应位置,当然这是你想放哪就放哪,只要你觉得美观就好,我们都习惯放文章上方,header的下方。。。
ul.breadcrumbs { list-style: none; padding: 0; margin: 0; font-size:12px; } ul.breadcrumbs li { float: left; margin: 0 5px 0 0; padding: 0; }
再将这段css放进主题文件下的css里即可。。。
这样wordpress面包屑导航的功能基本就大功告成了