|
你在浏览某个分类下的文章的时候你想回到上面的目录,就需要用到面包屑导航,它主要用来记录你当前相对于文章首页的位置,他是一个相对位置。
1,在functions.php中添加如下代码- function get_breadcrumbs()
- {
- global $wp_query;
- if ( !is_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 "» 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 "<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>";
- }
- }
复制代码 2,在需要调用的地方调用就好了- <?php
- get_breadcrumbs();
- ?>
复制代码 3,自己CSS美化一下吧
|
|