Advertisement
afsarwebdev

WP Breadcrumb complete file

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