Guest User

Untitled

a guest
Jan 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. <?php
  2.  
  3. // Breadcrumbs
  4. function custom_breadcrumbs() {
  5.  
  6. // Settings
  7. $separator = '|';
  8. $breadcrums_id = 'breadcrumbs';
  9. $breadcrums_class = 'breadcrumbs';
  10.  
  11. // If you have any custom post types with custom taxonomies, put the taxonomy name below (e.g. product_cat)
  12. $custom_taxonomy = 'product_cat';
  13.  
  14. // Get the query & post information
  15. global $post, $wp_query;
  16.  
  17. // Do not display on the homepage
  18. if ( !is_front_page() ) {
  19.  
  20. // Build the breadcrums
  21. echo '<ul id="' . $breadcrums_id . '" class="' . $breadcrums_class . '">';
  22.  
  23. if ( is_archive() && !is_tax() && !is_category() && !is_tag() ) {
  24.  
  25. echo '<li class="item-current item-archive" ><strong class="bread-current bread-archive">' . post_type_archive_title($prefix, false) . '</strong></li>';
  26.  
  27. } else if ( is_archive() && is_tax() && !is_category() && !is_tag() ) {
  28.  
  29. // If post is a custom post type
  30. $post_type = get_post_type();
  31.  
  32. // If it is a custom post type display name and link
  33. if($post_type != 'post') {
  34.  
  35. $post_type_object = get_post_type_object($post_type);
  36. $post_type_archive = get_post_type_archive_link($post_type);
  37.  
  38. echo '<li property="itemListElement" typeof="ListItem" 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>';
  39. echo '<li class="separator"> ' . $separator . ' </li>';
  40.  
  41. }
  42.  
  43. $custom_tax_name = get_queried_object()->name;
  44. echo '<li class="item-current item-archive"><strong class="bread-current bread-archive">' . $custom_tax_name . '</strong></li>';
  45.  
  46. } else if ( is_single() ) {
  47.  
  48. // If post is a custom post type
  49. $post_type = get_post_type();
  50.  
  51. // If it is a custom post type display name and link
  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 property="itemListElement" typeof="ListItem" 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 info
  63. $category = get_the_category();
  64.  
  65. if(!empty($category)) {
  66.  
  67. // Get last category post is in
  68. $last_category = end(array_values($category));
  69.  
  70. // Get 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. // If it's a custom post type within a custom taxonomy
  84. $taxonomy_exists = taxonomy_exists($custom_taxonomy);
  85. if(empty($last_category) && !empty($custom_taxonomy) && $taxonomy_exists) {
  86.  
  87. $taxonomy_terms = get_the_terms( $post->ID, $custom_taxonomy );
  88. $cat_id = $taxonomy_terms[0]->term_id;
  89. $cat_nicename = $taxonomy_terms[0]->slug;
  90. $cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy);
  91. $cat_name = $taxonomy_terms[0]->name;
  92.  
  93. }
  94.  
  95. // Check if the post is in a category
  96. if(!empty($last_category)) {
  97. echo $cat_display;
  98. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  99.  
  100. // Else if post is in a custom taxonomy
  101. } else if(!empty($cat_id)) {
  102.  
  103. echo '<li property="itemListElement" typeof="ListItem" 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>';
  104. echo '<li class="separator"> ' . $separator . ' </li>';
  105. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  106.  
  107. } else {
  108.  
  109. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
  110.  
  111. }
  112.  
  113. } else if ( is_category() ) {
  114.  
  115. // Category page
  116. echo '<li class="item-current item-cat"><strong class="bread-current bread-cat">' . single_cat_title('', false) . '</strong></li>';
  117.  
  118. } else if ( is_page() ) {
  119.  
  120. // Standard page
  121. if( $post->post_parent ){
  122.  
  123. // If child page, get parents
  124. $anc = get_post_ancestors( $post->ID );
  125.  
  126. // Get parents in the right order
  127. $anc = array_reverse($anc);
  128.  
  129. // Parent page loop
  130. if ( !isset( $parents ) ) $parents = null;
  131. foreach ( $anc as $ancestor ) {
  132. $parents .= '<li property="itemListElement" typeof="ListItem" 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>';
  133. $parents .= '<li class="separator separator-' . $ancestor . '"> ' . $separator . ' </li>';
  134. }
  135.  
  136. // Display parent pages
  137. echo $parents;
  138.  
  139. // Current page
  140. echo '<li class="item-current item-' . $post->ID . '"><strong title="' . get_the_title() . '"> ' . get_the_title() . '</strong></li>';
  141.  
  142. } else {
  143.  
  144. // Just display current page if not parents
  145. echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '"> ' . get_the_title() . '</strong></li>';
  146.  
  147. }
  148.  
  149. } else if ( is_tag() ) {
  150.  
  151. // Tag page
  152.  
  153. // Get tag information
  154. $term_id = get_query_var('tag_id');
  155. $taxonomy = 'post_tag';
  156. $args = 'include=' . $term_id;
  157. $terms = get_terms( $taxonomy, $args );
  158. $get_term_id = $terms[0]->term_id;
  159. $get_term_slug = $terms[0]->slug;
  160. $get_term_name = $terms[0]->name;
  161.  
  162. // Display the tag name
  163. 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>';
  164.  
  165. } elseif ( is_day() ) {
  166.  
  167. // Day archive
  168.  
  169. // Year link
  170. 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>';
  171. echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . $separator . ' </li>';
  172.  
  173. // Month link
  174. 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>';
  175. echo '<li class="separator separator-' . get_the_time('m') . '"> ' . $separator . ' </li>';
  176.  
  177. // Day display
  178. 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>';
  179.  
  180. } else if ( is_month() ) {
  181.  
  182. // Month Archive
  183.  
  184. // Year link
  185. 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>';
  186. echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . $separator . ' </li>';
  187.  
  188. // Month display
  189. 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>';
  190.  
  191. } else if ( is_year() ) {
  192.  
  193. // Display year archive
  194. 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>';
  195.  
  196. } else if ( is_author() ) {
  197.  
  198. // Auhor archive
  199.  
  200. // Get the author information
  201. global $author;
  202. $userdata = get_userdata( $author );
  203.  
  204. // Display author name
  205. 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>';
  206.  
  207. } else if ( get_query_var('paged') ) {
  208.  
  209. // Paginated archives
  210. 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>';
  211.  
  212. } else if ( is_search() ) {
  213.  
  214. // Search results page
  215. 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>';
  216.  
  217. } elseif ( is_404() ) {
  218.  
  219. // 404 page
  220. echo '<li>' . 'Error 404' . '</li>';
  221. }
  222.  
  223. echo '</ul>';
  224.  
  225. }
  226.  
  227. }
Add Comment
Please, Sign In to add comment