QQ咨询不加好友发不了信息,咨询前先加好友! → QQ:820896380

为WordPress添加一个面包屑导航

如何在你的Wordpres中为页面添加面包屑导航,支持自定义帖子类型、自定义分类。 但貌似在分类归档页面不能显示父子分类层级有点遗憾。 代码实现 将代码添加到当前主题函数模板functions.php中: /** * WordPress Breadcrumbs */ function tsh_wp_custom_bre...

如何在你的Wordpres中为页面添加面包屑导航,支持自定义帖子类型、自定义分类。

但貌似在分类归档页面不能显示父子分类层级有点遗憾。

代码实现

将代码添加到当前主题函数模板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. // Hide from front page
  16. if ( !is_front_page() ) {
  17. echo '<ul id="' . $breadcrumbs_id . '" class="' . $breadcrumbs_class . '">';
  18. // Home
  19. echo '<li class="item-home"><a class="bread-link bread-home" href="'%20.%20get_home_url()%20.%20'" title="' . $home_title . '">' . $home_title . '</a></li>';
  20. echo '<li class="separator separator-home"> ' . $separator . ' </li>';
  21. if ( is_archive() && !is_tax() && !is_category() && !is_tag() ) {
  22. echo '<li class="item-current item-archive"><strong class="bread-current bread-archive">' . post_type_archive_title('', false) . '</strong></li>';
  23. } else if ( is_archive() && is_tax() && !is_category() && !is_tag() ) {
  24. // For Custom post type
  25. $post_type = get_post_type();
  26. // Custom post type name and link
  27. if($post_type != 'post') {
  28. $post_type_object = get_post_type_object($post_type);
  29. $post_type_archive = get_post_type_archive_link($post_type);
  30. echo '<li class="item-cat item-custom-post-type-' . $post_type . '"><a class="bread-cat bread-custom-post-type-' . $post_type . '" href="'%20.%20$post_type_archive%20.%20'" title="' . $post_type_object->labels->name . '">' . $post_type_object->labels->name . '</a></li>';
  31. echo '<li class="separator"> ' . $separator . ' </li>';
  32. }
  33. $custom_tax_name = get_queried_object()->name;
  34. echo '<li class="item-current item-archive"><strong class="bread-current bread-archive">' . $custom_tax_name . '</strong></li>';
  35. } else if ( is_single() ) {
  36. $post_type = get_post_type();
  37.  
  38. if($post_type != 'post') {
  39. $post_type_object = get_post_type_object($post_type);
  40. $post_type_archive = get_post_type_archive_link($post_type);
  41. echo '<li class="item-cat item-custom-post-type-' . $post_type . '"><a class="bread-cat bread-custom-post-type-' . $post_type . '" href="'%20.%20$post_type_archive%20.%20'" title="' . $post_type_object->labels->name . '">' . $post_type_object->labels->name . '</a></li>';
  42. echo '<li class="separator"> ' . $separator . ' </li>';
  43. }
  44. // Get post category
  45. $category = get_the_category();
  46. if(!empty($category)) {
  47. // Last category post is in
  48. $last_category = $category[count($category) - 1];
  49. // Parent any categories and create array
  50. $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),',');
  51. $cat_parents = explode(',',$get_cat_parents);
  52. // Loop through parent categories and store in variable $cat_display
  53. $cat_display = '';
  54. foreach($cat_parents as $parents) {
  55. $cat_display .= '<li class="item-cat">'.$parents.'</li>';
  56. $cat_display .= '<li class="separator"> ' . $separator . ' </li>';
  57. }
  58. }
  59.  
  60. $taxonomy_exists = taxonomy_exists($tsh_custom_taxonomy);
  61. if(empty($last_category) && !empty($tsh_custom_taxonomy) && $taxonomy_exists) {
  62. $taxonomy_terms = get_the_terms( $post->ID, $tsh_custom_taxonomy );
  63. $cat_id = $taxonomy_terms[0]->term_id;
  64. $cat_nicename = $taxonomy_terms[0]->slug;
  65. $cat_link = get_term_link($taxonomy_terms[0]->term_id, $tsh_custom_taxonomy);
  66. $cat_name = $taxonomy_terms[0]->name;
  67. }
  68. // If the post is in a category
  69. if(!empty($last_category)) {
  70. echo $cat_display;
  71. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  72. // Post is in a custom taxonomy
  73. } else if(!empty($cat_id)) {
  74. 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="'%20.%20$cat_link%20.%20'" title="' . $cat_name . '">' . $cat_name . '</a></li>';
  75. echo '<li class="separator"> ' . $separator . ' </li>';
  76. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  77. } else {
  78. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  79. }
  80. } else if ( is_category() ) {
  81. // Category page
  82. echo '<li class="item-current item-cat"><strong class="bread-current bread-cat">' . single_cat_title('', false) . '</strong></li>';
  83. } else if ( is_page() ) {
  84. // Standard page
  85. if( $post->post_parent ){
  86. // Get parents
  87. $anc = get_post_ancestors( $post->ID );
  88. // Get parents order
  89. $anc = array_reverse($anc);
  90. // Parent pages
  91. if ( !isset( $parents ) ) $parents = null;
  92. foreach ( $anc as $ancestor ) {
  93. $parents .= '<li class="item-parent item-parent-' . $ancestor . '"><a class="bread-parent bread-parent-' . $ancestor . '" href="'%20.%20get_permalink($ancestor)%20.%20'" title="' . get_the_title($ancestor) . '">' . get_the_title($ancestor) . '</a></li>';
  94. $parents .= '<li class="separator separator-' . $ancestor . '"> ' . $separator . ' </li>';
  95. }
  96. // Render parent pages
  97. echo $parents;
  98. // Active page
  99. echo '<li class="item-current item-' . $post->ID . '"><strong title="' . get_the_title() . '"> ' . get_the_title() . '</strong></li>';
  100. } else {
  101. // Just display active page if not parents pages
  102. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '"> ' . get_the_title() . '</strong></li>';
  103. }
  104. } else if ( is_tag() ) { // Tag page
  105. // Tag information
  106. $term_id = get_query_var('tag_id');
  107. $taxonomy = 'post_tag';
  108. $args = 'include=' . $term_id;
  109. $terms = get_terms( $taxonomy, $args );
  110. $get_term_id = $terms[0]->term_id;
  111. $get_term_slug = $terms[0]->slug;
  112. $get_term_name = $terms[0]->name;
  113. // Return tag name
  114. 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>';
  115. } elseif ( is_day() ) { // Day archive page
  116. // Year link
  117. echo '<li class="item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" href="'%20.%20get_year_link(%20get_the_time('Y')%20)%20.%20'" title="' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</a></li>';
  118. echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . $separator . ' </li>';
  119. // Month link
  120. echo '<li class="item-month item-month-' . get_the_time('m') . '"><a class="bread-month bread-month-' . get_the_time('m') . '" href="'%20.%20get_month_link(%20get_the_time('Y'),%20get_the_time('m')%20)%20.%20'" title="' . get_the_time('M') . '">' . get_the_time('M') . ' Archives</a></li>';
  121. echo '<li class="separator separator-' . get_the_time('m') . '"> ' . $separator . ' </li>';
  122. // Day display
  123. 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>';
  124. } else if ( is_month() ) { // Month Archive
  125. // Year link
  126. echo '<li class="item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" href="'%20.%20get_year_link(%20get_the_time('Y')%20)%20.%20'" title="' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</a></li>';
  127. echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . $separator . ' </li>';
  128. // Month display
  129. 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>';
  130. } else if ( is_year() ) { // Display year archive
  131.  
  132. 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>';
  133. } else if ( is_author() ) { // Author archive
  134. // Get the author information
  135. global $author;
  136. $userdata = get_userdata( $author );
  137. // Display author name
  138. 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>';
  139. } else if ( get_query_var('paged') ) {
  140. // Paginated archives
  141. 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>';
  142. } else if ( is_search() ) {
  143. // Search results page
  144. 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>';
  145. } elseif ( is_404() ) {
  146. // 404 page
  147. echo '<li>' . 'Error 404' . '</li>';
  148. }
  149.  
  150. echo '</ul>';
  151. }
  152. }

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

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

配套CSS样式:

  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. }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

请我们喝杯咖啡,谢谢^_^

给TA打赏
共0人
如本文“对您有用”,欢迎随意打赏,金额不重要,认可最重要!
    豆包可以帮你高效完成AI问答、AI对话、提供软件相关教程以及解决生活中遇到的各种疑难杂症,还能帮助你进行AI写作、AI绘画等等,提高你的工作学习效率。
    !
    你也想出现在这里?立即 联系我们吧!
    信息
    个人中心
    购物车
    优惠劵
    今日签到
    搜索