第一步:在functions.php中添加如下代码
1 // 面包屑导航 2 function get_breadcrumbs() 3 { 4 global $wp_query; 5 if ( !is_home() ){ 6 // Start the UL 7 echo '<ul id="crumbs">'; 8 // Add the Home link 9 echo '<li><a href="'. get_settings('home') .'">'. get_bloginfo('name') .'</a></li>'; 10 if ( is_category() ) 11 { 12 $catTitle = single_cat_title( "", false ); 13 $cat = get_cat_ID( $catTitle ); 14 echo "<li> > ". get_category_parents( $cat, TRUE, ">" ) ."</li>"; 15 } 16 elseif ( is_archive() && !is_category() ) 17 { 18 echo "<li> > Archives</li>"; 19 } 20 elseif ( is_search() ) { 21 echo "<li> > Search Results</li>"; 22 } 23 elseif ( is_404() ) 24 { 25 echo "<li> > 404 Not Found</li>"; 26 } 27 elseif ( is_single() ) 28 { 29 $category = get_the_category(); 30 $category_id = get_cat_ID( $category[0]->cat_name ); 31 echo '<li> >'. get_category_parents( $category_id, TRUE, "" ); 32 //echo the_title('','', FALSE) ."</li>";如果需要把文章标题也显示出来,把这行显示出来就行 33 } 34 elseif ( is_page() ) 35 { 36 $post = $wp_query->get_queried_object(); 37 if ( $post->post_parent == 0 ){ 38 echo "<li> » ".the_title('','', FALSE)."</li>"; 39 } else { 40 $title = the_title('','', FALSE); 41 $ancestors = array_reverse( get_post_ancestors( $post->ID ) ); 42 array_push($ancestors, $post->ID); 43 foreach ( $ancestors as $ancestor ){ 44 if( $ancestor != end($ancestors) ){ 45 echo '<li> » <a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a></li>'; 46 } else { 47 echo '<li> » '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</li>'; 48 } 49 } 50 } 51 } 52 // End the UL 53 echo "</ul>"; 54 } 55 }
第二步:在需要使用面包屑的地方添加如下代码
<?php get_breadcrumbs(); ?>