实现wordpress面包屑导航的功能

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

教程开始了:   可自定义所有的判断选项

  1. function get_breadcrumbs()  
  2. {  
  3.     global $wp_query;  
  4.     
  5.     if ( !is_home() ){  
  6.     
  7.         // Start the UL  
  8.         echo 'class="breadcrumbs">';  
  9.         // Add the Home link  
  10.         echo '
  11. "'. get_settings('home') .'">'. get_bloginfo('name') .'
  12. ';  
  13.     
  14.         if ( is_category() )  
  15.         {  
  16.             $catTitle = single_cat_title( "", false );  
  17.             $cat = get_cat_ID( $catTitle );  
  18.             echo "
  19.  » ". get_category_parents( $cat, TRUE, " » " ) ."
  20. ";  
  21.         }  
  22.         elseif ( is_archive() && !is_category() )  
  23.         {  
  24.             echo "
  25.  » Archives
  26. ";  
  27.         }  
  28.         elseif ( is_search() ) {  
  29.     
  30.             echo "
  31.  » Search Results
  32. ";  
  33.         }  
  34.         elseif ( is_404() )  
  35.         {  
  36.             echo "
  37.  » 404 Not Found
  38. ";  
  39.         }  
  40.         elseif ( is_single() )  
  41.         {  
  42.             $category = get_the_category();  
  43.             $category_id = get_cat_ID( $category[0]->cat_name );  
  44.     
  45.             echo '
  46.  » '. get_category_parents( $category_id, TRUE, " » " );  
  47.             echo the_title('','', FALSE) ."
  48. ";  
  49.         }  
  50.         elseif ( is_page() )  
  51.         {  
  52.             $post = $wp_query->get_queried_object();  
  53.     
  54.             if ( $post->post_parent == 0 ){  
  55.     
  56.                 echo "
  57.  » ".the_title('','', FALSE)."
  58. ";  
  59.     
  60.             } else {  
  61.                 $title = the_title('','', FALSE);  
  62.                 $ancestors = array_reverse( get_post_ancestors( $post->ID ) );  
  63.                 array_push($ancestors$post->ID);  
  64.     
  65.                 foreach ( $ancestors as $ancestor ){  
  66.                     if$ancestor != end($ancestors) ){  
  67.                         echo '
  68.  » "'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'
  69. ';  
  70.                     } else {  
  71.                         echo '
  72.  » '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'
  73. ';  
  74.                     }  
  75.                 }  
  76.             }  
  77.         }  
  78.     
  79.         // End the UL  
  80.         echo "";  
  81.     }  
  82. }  

将上面的代码复制进wordpress主题文件夹下的functions.php中,然后,我们开始调用它

  1. if (function_exists('get_breadcrumbs')){get_breadcrumbs(); } ?>  
将上面的调用函数放进wordpress主题文件下的archive.php、single.php、index.php、search.php等页面的相应位置,当然这是你想放哪就放哪,只要你觉得美观就好

你可能感兴趣的:(wordpress)