WordPress面包屑导航代码

  • 酉灿
  • WordPress
  • Jul 23, 2021

转载分享一段WordPress面包屑导航代码,支持自定义帖子类型、自定义分类,但貌似在分类归档页面不能显示父子分类层级有点遗憾。

将代码添加到当前主题函数模板functions.php中:


 
  1. /**
  2. * WordPress Breadcrumbs
  3. */
  4. function tsh_wp_custom_breadcrumbs() {
  5.  
  6. $separator = '/';
  7. $breadcrumbs_id = 'tsh_breadcrumbs';
  8. $breadcrumbs_class = 'tsh_breadcrumbs';
  9. $home_title = esc_html__('Home', 'your-domain');
  10.  
  11. // Add here you custom post taxonomies
  12. $tsh_custom_taxonomy = 'product_cat';
  13.  
  14. global $post,$wp_query;
  15.  
  16. // Hide from front page
  17. if ( !is_front_page() ) {
  18.  
  19. echo '<ul id="' . $breadcrumbs_id . '" class="' . $breadcrumbs_class . '">';
  20.  
  21. // Home
  22. echo '<li class="item-home"><a class="bread-link bread-home" href="' . get_home_url() . '" title="' . $home_title . '">' . $home_title . '</a></li>';
  23. echo '<li class="separator separator-home"> ' . $separator . ' </li>';
  24.  
  25. if ( is_archive() && !is_tax() && !is_category() && !is_tag() ) {
  26.  
  27. echo '<li class="item-current item-archive"><strong class="bread-current bread-archive">' . post_type_archive_title('', false) . '</strong></li>';
  28.  
  29. } else if ( is_archive() && is_tax() && !is_category() && !is_tag() ) {
  30.  
  31. // For Custom post type
  32. $post_type = get_post_type();
  33.  
  34. // Custom post type name and link
  35. if($post_type != 'post') {
  36.  
  37. $post_type_object = get_post_type_object($post_type);
  38. $post_type_archive = get_post_type_archive_link($post_type);
  39.  
  40. echo '<li class="item-cat item-custom-post-type-' . $post_type . '"><a class="bread-cat bread-custom-post-type-' . $post_type . '" href="' . $post_type_archive . '" title="' . $post_type_object->labels->name . '">' . $post_type_object->labels->name . '</a></li>';
  41. echo '<li class="separator"> ' . $separator . ' </li>';
  42.  
  43. }
  44.  
  45. $custom_tax_name = get_queried_object()->name;
  46. echo '<li class="item-current item-archive"><strong class="bread-current bread-archive">' . $custom_tax_name . '</strong></li>';
  47.  
  48. } else if ( is_single() ) {
  49.  
  50. $post_type = get_post_type();
  51.  
  52. if($post_type != 'post') {
  53.  
  54. $post_type_object = get_post_type_object($post_type);
  55. $post_type_archive = get_post_type_archive_link($post_type);
  56.  
  57. echo '<li class="item-cat item-custom-post-type-' . $post_type . '"><a class="bread-cat bread-custom-post-type-' . $post_type . '" href="' . $post_type_archive . '" title="' . $post_type_object->labels->name . '">' . $post_type_object->labels->name . '</a></li>';
  58. echo '<li class="separator"> ' . $separator . ' </li>';
  59.  
  60. }
  61.  
  62. // Get post category
  63. $category = get_the_category();
  64.  
  65. if(!empty($category)) {
  66.  
  67. // Last category post is in
  68. $last_category = $category[count($category) - 1];
  69.  
  70. // Parent any categories and create array
  71. $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),',');
  72. $cat_parents = explode(',',$get_cat_parents);
  73.  
  74. // Loop through parent categories and store in variable $cat_display
  75. $cat_display = '';
  76. foreach($cat_parents as $parents) {
  77. $cat_display .= '<li class="item-cat">'.$parents.'</li>';
  78. $cat_display .= '<li class="separator"> ' . $separator . ' </li>';
  79. }
  80.  
  81. }
  82.  
  83. $taxonomy_exists = taxonomy_exists($tsh_custom_taxonomy);
  84. if(empty($last_category) && !empty($tsh_custom_taxonomy) && $taxonomy_exists) {
  85.  
  86. $taxonomy_terms = get_the_terms( $post->ID, $tsh_custom_taxonomy );
  87. $cat_id = $taxonomy_terms[0]->term_id;
  88. $cat_nicename = $taxonomy_terms[0]->slug;
  89. $cat_link = get_term_link($taxonomy_terms[0]->term_id, $tsh_custom_taxonomy);
  90. $cat_name = $taxonomy_terms[0]->name;
  91.  
  92. }
  93.  
  94. // If the post is in a category
  95. if(!empty($last_category)) {
  96. echo $cat_display;
  97. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  98.  
  99. // Post is in a custom taxonomy
  100. } else if(!empty($cat_id)) {
  101.  
  102. echo '<li class="item-cat item-cat-' . $cat_id . ' item-cat-' . $cat_nicename . '"><a class="bread-cat bread-cat-' . $cat_id . ' bread-cat-' . $cat_nicename . '" href="' . $cat_link . '" title="' . $cat_name . '">' . $cat_name . '</a></li>';
  103. echo '<li class="separator"> ' . $separator . ' </li>';
  104. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  105.  
  106. } else {
  107.  
  108. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  109.  
  110. }
  111.  
  112. } else if ( is_category() ) {
  113.  
  114. // Category page
  115. echo '<li class="item-current item-cat"><strong class="bread-current bread-cat">' . single_cat_title('', false) . '</strong></li>';
  116.  
  117. } else if ( is_page() ) {
  118.  
  119. // Standard page
  120. if( $post->post_parent ){
  121.  
  122. // Get parents
  123. $anc = get_post_ancestors( $post->ID );
  124.  
  125. // Get parents order
  126. $anc = array_reverse($anc);
  127.  
  128. // Parent pages
  129. if ( !isset( $parents ) ) $parents = null;
  130. foreach ( $anc as $ancestor ) {
  131. $parents .= '<li class="item-parent item-parent-' . $ancestor . '"><a class="bread-parent bread-parent-' . $ancestor . '" href="' . get_permalink($ancestor) . '" title="' . get_the_title($ancestor) . '">' . get_the_title($ancestor) . '</a></li>';
  132. $parents .= '<li class="separator separator-' . $ancestor . '"> ' . $separator . ' </li>';
  133. }
  134.  
  135. // Render parent pages
  136. echo $parents;
  137.  
  138. // Active page
  139. echo '<li class="item-current item-' . $post->ID . '"><strong title="' . get_the_title() . '"> ' . get_the_title() . '</strong></li>';
  140.  
  141. } else {
  142.  
  143. // Just display active page if not parents pages
  144. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '"> ' . get_the_title() . '</strong></li>';
  145.  
  146. }
  147.  
  148. } else if ( is_tag() ) { // Tag page
  149.  
  150. // Tag information
  151. $term_id = get_query_var('tag_id');
  152. $taxonomy = 'post_tag';
  153. $args = 'include=' . $term_id;
  154. $terms = get_terms( $taxonomy, $args );
  155. $get_term_id = $terms[0]->term_id;
  156. $get_term_slug = $terms[0]->slug;
  157. $get_term_name = $terms[0]->name;
  158.  
  159. // Return tag name
  160. echo '<li class="item-current item-tag-' . $get_term_id . ' item-tag-' . $get_term_slug . '"><strong class="bread-current bread-tag-' . $get_term_id . ' bread-tag-' . $get_term_slug . '">' . $get_term_name . '</strong></li>';
  161.  
  162. } elseif ( is_day() ) { // Day archive page
  163.  
  164. // Year link
  165. echo '<li class="item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" href="' . get_year_link( get_the_time('Y') ) . '" title="' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</a></li>';
  166. echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . $separator . ' </li>';
  167.  
  168. // Month link
  169. echo '<li class="item-month item-month-' . get_the_time('m') . '"><a class="bread-month bread-month-' . get_the_time('m') . '" href="' . get_month_link( get_the_time('Y'), get_the_time('m') ) . '" title="' . get_the_time('M') . '">' . get_the_time('M') . ' Archives</a></li>';
  170. echo '<li class="separator separator-' . get_the_time('m') . '"> ' . $separator . ' </li>';
  171.  
  172. // Day display
  173. echo '<li class="item-current item-' . get_the_time('j') . '"><strong class="bread-current bread-' . get_the_time('j') . '"> ' . get_the_time('jS') . ' ' . get_the_time('M') . ' Archives</strong></li>';
  174.  
  175. } else if ( is_month() ) { // Month Archive
  176.  
  177. // Year link
  178. echo '<li class="item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" href="' . get_year_link( get_the_time('Y') ) . '" title="' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</a></li>';
  179. echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . $separator . ' </li>';
  180.  
  181. // Month display
  182. echo '<li class="item-month item-month-' . get_the_time('m') . '"><strong class="bread-month bread-month-' . get_the_time('m') . '" title="' . get_the_time('M') . '">' . get_the_time('M') . ' Archives</strong></li>';
  183.  
  184. } else if ( is_year() ) { // Display year archive
  185.  
  186. echo '<li class="item-current item-current-' . get_the_time('Y') . '"><strong class="bread-current bread-current-' . get_the_time('Y') . '" title="' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</strong></li>';
  187.  
  188. } else if ( is_author() ) { // Author archive
  189.  
  190. // Get the author information
  191. global $author;
  192. $userdata = get_userdata( $author );
  193.  
  194. // Display author name
  195. echo '<li class="item-current item-current-' . $userdata->user_nicename . '"><strong class="bread-current bread-current-' . $userdata->user_nicename . '" title="' . $userdata->display_name . '">' . 'Author: ' . $userdata->display_name . '</strong></li>';
  196.  
  197. } else if ( get_query_var('paged') ) {
  198.  
  199. // Paginated archives
  200. echo '<li class="item-current item-current-' . get_query_var('paged') . '"><strong class="bread-current bread-current-' . get_query_var('paged') . '" title="Page ' . get_query_var('paged') . '">'.__('Page') . ' ' . get_query_var('paged') . '</strong></li>';
  201.  
  202. } else if ( is_search() ) {
  203.  
  204. // Search results page
  205. echo '<li class="item-current item-current-' . get_search_query() . '"><strong class="bread-current bread-current-' . get_search_query() . '" title="Search results for: ' . get_search_query() . '">Search results for: ' . get_search_query() . '</strong></li>';
  206.  
  207. } elseif ( is_404() ) {
  208.  
  209. // 404 page
  210. echo '<li>' . 'Error 404' . '</li>';
  211. }
  212.  
  213. echo '</ul>';
  214. }
  215. }

将调用代码放到主题模板适当位置比如header.php中:


 
  1. <?php if (function_exists('tsh_wp_custom_breadcrumbs')) tsh_wp_custom_breadcrumbs(); ?>

配套样式:


 
  1. #tsh_breadcrumbs .separator{
  2. font-size:20px;
  3. color:#ccc;
  4. font-weight:100;
  5. }
  6. #tsh_breadcrumbs{
  7. overflow:hidden;
  8. text-align: center;
  9. list-style:none;
  10. margin:11px 0;
  11. }
  12. #tsh_breadcrumbs li{
  13. margin-right:14px;
  14. display:inline-block;
  15. vertical-align:middle;
  16. }

 

 

 
打赏