Advertisement
afsarwebdev

Breadcrumbs

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