layers_bread_crumbs

The Layers layers bread crumbs function.

Description

layers_bread_crumbs( (string) $wrapper = 'nav', (string) $wrapper_class = 'bread-crumbs', (string) $seperator = '/' ); 

Parameters (3)

0. $wrapper — Optional. (string) => 'nav'
The wrapper.
1. $wrapper_class — Optional. (string) => 'bread-crumbs'
The wrapper class.
2. $seperator — Optional. (string) => '/'
The seperator.

Usage

  1. if ( !function_exists( 'layers_bread_crumbs' ) ) { 
  2. require_once '/core/helpers/template.php'; 
  3.  
  4. // The wrapper. 
  5. $wrapper = 'nav'; 
  6.  
  7. // The wrapper class. 
  8. $wrapper_class = 'bread-crumbs'; 
  9.  
  10. // The seperator. 
  11. $seperator = '/'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = layers_bread_crumbs($wrapper, $wrapper_class, $seperator); 
  15.  

Defined (1)

The function is defined in the following location(s).

/core/helpers/template.php  
  1. function layers_bread_crumbs( $wrapper = 'nav', $wrapper_class = 'bread-crumbs', $seperator = '/' ) { 
  2. global $post; ?> 
  3. <<?php echo $wrapper; ?> class="<?php echo esc_attr( $wrapper_class ); ?>"> 
  4. <ul> 
  5. <?php /** Home */ ?> 
  6. <li><a href="<?php echo home_url(); ?>"><?php _e('Home' , 'layerswp' ); ?></a></li> 
  7.  
  8. <?php 
  9.  
  10. /** Base Page 
  11. - Shop 
  12. - Search 
  13. - Post type parent page 
  14. */ 
  15. if( is_search() ) { ?> 
  16. <li><?php echo esc_html( $seperator ); ?></li> 
  17. <li><?php _e('Search' , 'layerswp' ); ?></li> 
  18. <?php } elseif( function_exists('is_shop') && ( is_post_type_archive( 'product' ) || ( get_post_type() == "product") ) ) { ?> 
  19. <li><?php echo esc_html( $seperator ); ?></li> 
  20. <?php if( function_exists( 'woocommerce_get_page_id' ) && '-1' != woocommerce_get_page_id('shop') ) { ?> 
  21. <?php $shop_page_id = woocommerce_get_page_id('shop'); ?> 
  22. <?php $shop_page = get_post( $shop_page_id ); ?> 
  23. <?php if( is_object ( $shop_page ) ) { ?> 
  24. <li><a href="<?php echo get_permalink( $shop_page->ID ); ?>"><?php echo $shop_page->post_title; ?></a></li> 
  25. <?php } ?> 
  26. <?php } else { ?> 
  27. <li><a href="#"><?php _e( 'Shop' , 'layerswp' ); ?></a></li> 
  28. <?php } 
  29. } elseif( is_post_type_archive() || is_singular() || is_tax() ) { 
  30.  
  31. // Get the post type object 
  32. $post_type = get_post_type_object( get_post_type() ); 
  33.  
  34. // Check if we have the relevant information we need to query the page 
  35. if( !empty( $post_type ) && isset( $post_type->labels->slug ) ) { 
  36.  
  37. // Query template 
  38. $parentpage = get_template_link( $post_type->labels->slug .".php"); 
  39.  
  40. // Display page if it has been found 
  41. if( !empty( $parentpage ) ) { ?> 
  42. <li><?php echo esc_html( $seperator ); ?></li> 
  43. <li><a href="<?php echo get_permalink($parentpage->ID); ?>"><?php echo $parentpage->post_title; ?></a></li> 
  44. <?php } 
  45. }; 
  46.  
  47.  
  48. /** Categories, Taxonomies & Parent Pages 
  49.   
  50. - Page parents 
  51. - Category & Taxonomy parents 
  52. - Category for current post 
  53. - Taxonomy for current post 
  54. */ 
  55.  
  56. if( is_page() ) { 
  57.  
  58. // Start with this page's parent ID 
  59. $parent_id = $post->post_parent; 
  60.  
  61. // Loop through parent pages and grab their IDs 
  62. while( $parent_id ) { 
  63. $page = get_post($parent_id); 
  64. $parent_pages[] = $page->ID; 
  65. $parent_id = $page->post_parent; 
  66.  
  67. // If there are parent pages, output them 
  68. if( isset( $parent_pages ) && is_array($parent_pages) ) { 
  69. $parent_pages = array_reverse($parent_pages); 
  70. foreach ( $parent_pages as $page_id ) { ?> 
  71. <!-- Parent page title --> 
  72. <li><?php echo esc_html( $seperator ); ?></li> 
  73. <li><a href="<?php echo get_permalink( $page_id ); ?>"><?php echo get_the_title( $page_id ); ?></a></li> 
  74. <?php } 
  75.  
  76. } elseif( is_category() || is_tax() ) { 
  77.  
  78. // Get the taxonomy object 
  79. if( is_category() ) { 
  80. $category_title = single_cat_title( "", false ); 
  81. $category_id = get_cat_ID( $category_title ); 
  82. $category_object = get_category( $category_id ); 
  83.  
  84. if( is_object( $category_object ) ) { 
  85. $term = $category_object->slug; 
  86. } else { 
  87. $term = ''; 
  88.  
  89. $taxonomy = 'category'; 
  90. $term_object = get_term_by( 'slug', $term , $taxonomy ); 
  91. } else { 
  92. $term = get_query_var('term' ); 
  93. $taxonomy = get_query_var( 'taxonomy' ); 
  94. $term_object = get_term_by( 'slug', $term , $taxonomy ); 
  95.  
  96. if( is_object( $term_object ) ) 
  97. $parent_id = $term_object->parent; 
  98. else 
  99. $parent_id = FALSE; 
  100.  
  101. // Start with this terms's parent ID 
  102.  
  103. // Loop through parent terms and grab their IDs 
  104. while( $parent_id ) { 
  105. $cat = get_term_by( 'id' , $parent_id , $taxonomy ); 
  106. $parent_terms[] = $cat->term_id; 
  107. $parent_id = $cat->parent; 
  108.  
  109. // If there are parent terms, output them 
  110. if( isset( $parent_terms ) && is_array($parent_terms) ) { 
  111. $parent_terms = array_reverse($parent_terms); 
  112.  
  113. foreach ( $parent_terms as $term_id ) { 
  114. $term = get_term_by( 'id' , $term_id , $taxonomy ); ?> 
  115.  
  116. <li><?php echo esc_html( $seperator ); ?></li> 
  117. <li><a href="<?php echo get_term_link( $term_id , $taxonomy ); ?>"><?php echo $term->name; ?></a></li> 
  118.  
  119. <?php } 
  120.  
  121. } elseif ( is_single() && get_post_type() == 'post' ) { 
  122.  
  123. // Get all post categories but use the first one in the array 
  124. $category_array = get_the_category(); 
  125.  
  126. foreach ( $category_array as $category ) { ?> 
  127.  
  128. <li><?php echo esc_html( $seperator ); ?></li> 
  129. <li><a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo get_cat_name( $category->term_id ); ?></a></li> 
  130.  
  131. <?php } 
  132.  
  133. } elseif( is_singular() ) { 
  134.  
  135. // Get the post type object 
  136. $post_type = get_post_type_object( get_post_type() ); 
  137.  
  138. // If this is a product, make sure we're using the right term slug 
  139. if( is_post_type_archive( 'product' ) || ( get_post_type() == "product" ) ) { 
  140. $taxonomy = 'product_cat'; 
  141. } elseif( !empty( $post_type ) && isset( $post_type->taxonomies[0] ) ) { 
  142. $taxonomy = $post_type->taxonomies[0]; 
  143. }; 
  144.  
  145. if( isset( $taxonomy ) && !is_wp_error( $taxonomy ) ) { 
  146. // Get the terms 
  147. $terms = get_the_terms( get_the_ID(), $taxonomy ); 
  148.  
  149. // If this term is legal, proceed 
  150. if( is_array( $terms ) ) { 
  151.  
  152. // Loop over the terms for this post 
  153. foreach ( $terms as $term ) { ?> 
  154.  
  155. <li><?php echo esc_html( $seperator ); ?></li> 
  156. <li><a href="<?php echo get_term_link( $term->slug, $taxonomy ); ?>"><?php echo $term->name; ?></a></li> 
  157.  
  158. <?php } 
  159. } ?> 
  160.  
  161. <?php /** Current Page / Post / Post Type 
  162.   
  163. - Page / Page / Post type title 
  164. - Search term 
  165. - Curreny Taxonomy 
  166. - Current Tag 
  167. - Current Category 
  168. */ 
  169.  
  170. if( is_singular() ) { ?> 
  171.  
  172. <li><?php echo esc_html( $seperator ); ?></li> 
  173. <li><span class="current"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span></li> 
  174.  
  175. <?php } elseif ( is_search() ) { ?> 
  176.  
  177. <li><?php echo esc_html( $seperator ); ?></li> 
  178. <li><span class="current">"<?php echo get_search_query(); ?>"</span></li> 
  179.  
  180. <?php } elseif( is_tax() ) { 
  181.  
  182. // Get this term's details 
  183. $term = get_term_by( 'slug', get_query_var('term' ), get_query_var( 'taxonomy' ) ); ?> 
  184.  
  185. <li><?php echo esc_html( $seperator ); ?></li> 
  186. <li><span class="current"><?php echo $term->name; ?></span></li> 
  187.  
  188. <?php } elseif( is_tag() ) { ?> 
  189.  
  190. <li><?php echo esc_html( $seperator ); ?></li> 
  191. <li><span class="current"><?php echo single_tag_title(); ?></span></li> 
  192.  
  193. <?php } elseif( is_category() ) { ?> 
  194.  
  195. <li><?php echo esc_html( $seperator ); ?></li> 
  196. <li><span class="current"><?php echo single_cat_title(); ?></span></li> 
  197.  
  198. <?php } elseif ( is_archive() && is_month() ) { ?> 
  199.  
  200. <li><?php echo esc_html( $seperator ); ?></li> 
  201. <li><span class="current"><?php echo get_the_date( 'F Y' ); ?></span></li> 
  202.  
  203. <?php } elseif ( is_archive() && is_year() ) { ?> 
  204.  
  205. <li><?php echo esc_html( $seperator ); ?></li> 
  206. <li><span class="current"><?php echo get_the_date( 'Y' ); ?></span></li> 
  207.  
  208. <?php } elseif ( is_archive() && is_author() ) { ?> 
  209.  
  210. <li><?php echo esc_html( $seperator ); ?></li> 
  211. <li><span class="current"><?php echo get_the_author(); ?></span></li> 
  212.  
  213. <?php } ?> 
  214. </ul> 
  215. </<?php echo $wrapper; ?>> 
  216. <?php }