retesere20

wp-custom-breadcrumbs

Nov 28th, 2019
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. // https://wordpress.org/plugins/breadcrumb-trail/screenshots/
  4. public function custom_breadcrumbs() {
  5.  
  6. // Settings
  7. $separator = '>';
  8. $breadcrums_id = 'breadcrumbs';
  9. $breadcrums_class = 'breadcrumbs';
  10. $home_title = 'Homepage';
  11. $class = 'BrdClass';
  12.  
  13. // If you have any custom post types with custom taxonomies, put the taxonomy name below (e.g. product_cat)
  14. $custom_taxonomy = '';
  15.  
  16. // Get the query & post information
  17. global $post,$wp_query;
  18.  
  19. // Do not display on the homepage
  20. if ( !is_front_page() ) {
  21.  
  22. // Build the breadcrums
  23. echo '<ul id="' . $breadcrums_id . '" class="' . $class . '">';
  24.  
  25. // Home page
  26. echo '<li class="item-home"><a class="bread-link bread-home" href="' . get_home_url() . '" title="' . $home_title . '">' . $home_title . '</a></li>';
  27. echo '<li class="separator separator-home"> ' . $separator . ' </li>';
  28.  
  29. if ( is_archive() && !is_tax() ) {
  30.  
  31. echo '<li class="item-current item-archive"><strong class="bread-current bread-archive">' . post_type_archive_title($prefix, false) . '</strong></li>';
  32.  
  33. } else if ( is_archive() && is_tax() ) {
  34.  
  35. // If post is a custom post type
  36. $post_type = get_post_type();
  37.  
  38. // If it is a custom post type display name and link
  39. if($post_type != 'post') {
  40.  
  41. $post_type_object = get_post_type_object($post_type);
  42. $post_type_archive = get_post_type_archive_link($post_type);
  43.  
  44. 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>';
  45. echo '<li class="separator"> ' . $separator . ' </li>';
  46.  
  47. }
  48. $custom_tax_name = get_queried_object()->name;
  49. echo '<li class="item-current item-archive"><strong class="bread-current bread-archive">' . $custom_tax_name . '</strong></li>';
  50.  
  51. } else if ( is_single() ) {
  52.  
  53. // If post is a custom post type
  54. $post_type = get_post_type();
  55.  
  56. // If it is a custom post type display name and link
  57. if($post_type != 'post') {
  58.  
  59. $post_type_object = get_post_type_object($post_type);
  60. $post_type_archive = get_post_type_archive_link($post_type);
  61.  
  62. 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>';
  63. echo '<li class="separator"> ' . $separator . ' </li>';
  64.  
  65. }
  66.  
  67.  
  68. // Get post category info
  69. $category = get_the_category();
  70.  
  71. // Get last category post is in
  72. $last_category = end(array_values($category));
  73.  
  74. // Get parent any categories and create array
  75. $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),',');
  76. $cat_parents = explode(',',$get_cat_parents);
  77.  
  78. // Loop through parent categories and store in variable $cat_display
  79. $cat_display = '';
  80. foreach($cat_parents as $parents) {
  81. $cat_display .= '<li class="item-cat">'.$parents.'</li>';
  82. $cat_display .= '<li class="separator"> ' . $separator . ' </li>';
  83. }
  84.  
  85. // If it's a custom post type within a custom taxonomy
  86. if(empty($last_category) && !empty($custom_taxonomy)) {
  87.  
  88. $taxonomy_terms = get_the_terms( $post->ID, $custom_taxonomy );
  89. $cat_id = $taxonomy_terms[0]->term_id;
  90. $cat_nicename = $taxonomy_terms[0]->slug;
  91. $cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy);
  92. $cat_name = $taxonomy_terms[0]->name;
  93.  
  94. }
  95.  
  96. // Check if the post is in a category
  97. if(!empty($last_category)) {
  98. echo $cat_display;
  99. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  100.  
  101. // Else if post is in a custom taxonomy
  102. } else if(!empty($cat_id)) {
  103.  
  104. 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>';
  105. echo '<li class="separator"> ' . $separator . ' </li>';
  106. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  107.  
  108. }
  109.  
  110. } else if ( is_category() ) {
  111.  
  112. // Category page
  113. echo '<li class="item-current item-cat-' . $category[0]->term_id . ' item-cat-' . $category[0]->category_nicename . '"><strong class="bread-current bread-cat-' . $category[0]->term_id . ' bread-cat-' . $category[0]->category_nicename . '">' . $category[0]->cat_name . '</strong></li>';
  114.  
  115. } else if ( is_page() ) {
  116.  
  117. // Standard page
  118. if( $post->post_parent ){
  119.  
  120. // If child page, get parents
  121. $anc = get_post_ancestors( $post->ID );
  122.  
  123. // Get parents in the right order
  124. $anc = array_reverse($anc);
  125.  
  126. // Parent page loop
  127. foreach ( $anc as $ancestor ) {
  128. $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>';
  129. $parents .= '<li class="separator separator-' . $ancestor . '"> ' . $separator . ' </li>';
  130. }
  131.  
  132. // Display parent pages
  133. echo $parents;
  134.  
  135. // Current page
  136. echo '<li class="item-current item-' . $post->ID . '"><strong title="' . get_the_title() . '"> ' . get_the_title() . '</strong></li>';
  137.  
  138. } else {
  139.  
  140. // Just display current page if not parents
  141. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '"> ' . get_the_title() . '</strong></li>';
  142.  
  143. }
  144.  
  145. } else if ( is_tag() ) {
  146.  
  147. // Tag page
  148.  
  149. // Get tag information
  150. $term_id = get_query_var('tag_id');
  151. $taxonomy = 'post_tag';
  152. $args ='include=' . $term_id;
  153. $terms = get_terms( $taxonomy, $args );
  154.  
  155. // Display the tag name
  156. echo '<li class="item-current item-tag-' . $terms[0]->term_id . ' item-tag-' . $terms[0]->slug . '"><strong class="bread-current bread-tag-' . $terms[0]->term_id . ' bread-tag-' . $terms[0]->slug . '">' . $terms[0]->name . '</strong></li>';
  157.  
  158. } elseif ( is_day() ) {
  159.  
  160. // Day archive
  161.  
  162. // Year link
  163. 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>';
  164. echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . $separator . ' </li>';
  165.  
  166. // Month link
  167. 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>';
  168. echo '<li class="separator separator-' . get_the_time('m') . '"> ' . $separator . ' </li>';
  169.  
  170. // Day display
  171. 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>';
  172.  
  173. } else if ( is_month() ) {
  174.  
  175. // 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() ) {
  185.  
  186. // Display year archive
  187. 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>';
  188.  
  189. } else if ( is_author() ) {
  190.  
  191. // Auhor archive
  192.  
  193. // Get the author information
  194. global $author;
  195. $userdata = get_userdata( $author );
  196.  
  197. // Display author name
  198. 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>';
  199.  
  200. } else if ( get_query_var('paged') ) {
  201.  
  202. // Paginated archives
  203. 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>';
  204.  
  205. } else if ( is_search() ) {
  206.  
  207. // Search results page
  208. 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>';
  209.  
  210. } elseif ( is_404() ) {
  211.  
  212. // 404 page
  213. echo '<li>' . 'Error 404' . '</li>';
  214. }
  215.  
  216. echo '</ul>';
  217.  
  218. }
  219.  
  220. }
RAW Paste Data