onetone_breadcrumbs

The code below is an inspired/modified version by woothemes breadcrumb function which in turn is inspired by Justin Tadlock's Hybrid Core :).

Description

onetone_breadcrumbs( (array) $args = array() ); 

Parameters (1)

0. $args — Optional. (array) => array()
The args.

Usage

  1. if ( !function_exists( 'onetone_breadcrumbs' ) ) { 
  2. require_once '/includes/class-breadcrumb.php'; 
  3.  
  4. // The args. 
  5. $args = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = onetone_breadcrumbs($args); 
  9.  

Defined (1)

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

/includes/class-breadcrumb.php  
  1. function onetone_breadcrumbs( $args = array() ) { 
  2. global $wp_query, $wp_rewrite; 
  3.  
  4.  
  5. /** Create an empty variable for the breadcrumb. */ 
  6. $breadcrumb = ''; 
  7.  
  8. /** Create an empty array for the trail. */ 
  9. $trail = array(); 
  10. $path = ''; 
  11.  
  12. /** Set up the default arguments for the breadcrumb. */ 
  13. $defaults = array( 
  14. 'separator' => '»',  
  15. 'before' => '<span class="breadcrumb-title">' . __( 'You are here:', 'onetone') . '</span>',  
  16. 'after' => false,  
  17. 'front_page' => true,  
  18. 'show_home' => __( 'Home', 'onetone'),  
  19. 'echo' => false,  
  20. 'show_posts_page' => true 
  21. ); 
  22.  
  23.  
  24. /** Allow singular post views to have a taxonomy's terms prefixing the trail. */ 
  25. if ( is_singular() ) 
  26. $defaults["singular_{$wp_query->post->post_type}_taxonomy"] = false; 
  27.  
  28. /** Apply filters to the arguments. */ 
  29. $args = apply_filters( 'onetone_breadcrumbs_args', $args ); 
  30.  
  31. /** Parse the arguments and extract them for easy variable naming. */ 
  32. extract( wp_parse_args( $args, $defaults ) ); 
  33.  
  34. /** If $show_home is set and we're not on the front page of the site, link to the home page. */ 
  35. if ( !is_front_page() && $show_home ) 
  36. $trail[] = '<a href="' . esc_url(home_url('/')). '" title="' . esc_attr( get_bloginfo( 'name' ) ) . '" rel="home" class="trail-begin">' . $show_home . '</a>'; 
  37.  
  38. /** If viewing the front page of the site. */ 
  39. if ( is_front_page() ) { 
  40. if ( !$front_page ) 
  41. $trail = false; 
  42. elseif ( $show_home ) 
  43. $trail['trail_end'] = "{$show_home}"; 
  44.  
  45. /** If viewing the "home"/posts page. */ 
  46. elseif ( is_home() ) { 
  47. $home_page = get_page( $wp_query->get_queried_object_id() ); 
  48. $trail = array_merge( $trail, onetone_breadcrumbs_get_parents( $home_page->post_parent, '' ) ); 
  49. $trail['trail_end'] = get_the_title( $home_page->ID ); 
  50.  
  51. /** If viewing a singular post (page, attachment, etc.). */ 
  52. elseif ( is_singular() ) { 
  53.  
  54. /** Get singular post variables needed. */ 
  55. $post = $wp_query->get_queried_object(); 
  56. $post_id = absint( $wp_query->get_queried_object_id() ); 
  57. $post_type = $post->post_type; 
  58. $parent = $post->post_parent; 
  59.  
  60.  
  61. /** If a custom post type, check if there are any pages in its hierarchy based on the slug. */ 
  62. if ( 'page' !== $post_type && 'post' !== $post_type ) { 
  63.  
  64. $post_type_object = get_post_type_object( $post_type ); 
  65.  
  66. /** If $front has been set, add it to the $path. */ 
  67. if ( 'post' == $post_type || 'attachment' == $post_type || ( $post_type_object->rewrite['with_front'] && $wp_rewrite->front ) ) 
  68. $path .= trailingslashit( $wp_rewrite->front ); 
  69.  
  70. /** If there's a slug, add it to the $path. */ 
  71. if ( !empty( $post_type_object->rewrite['slug'] ) ) 
  72. $path .= $post_type_object->rewrite['slug']; 
  73.  
  74. /** If there's a path, check for parents. */ 
  75. if ( !empty( $path ) ) 
  76. $trail = array_merge( $trail, onetone_breadcrumbs_get_parents( '', $path ) ); 
  77.  
  78. /** If there's an archive page, add it to the trail. */ 
  79. if ( !empty( $post_type_object->has_archive ) && function_exists( 'get_post_type_archive_link' ) ) 
  80. $trail[] = '<a href="' . get_post_type_archive_link( $post_type ) . '" title="' . esc_attr( $post_type_object->labels->name ) . '">' . $post_type_object->labels->name . '</a>'; 
  81.  
  82. /** try to build a generic taxonomy trail no matter the post type and taxonomy and terms 
  83. $currentTax = ""; 
  84. foreach(get_taxonomies() as $tax) 
  85. $terms = get_the_term_list( $post_id, $tax, '', '$$$', '' ); 
  86. echo"<pre>"; 
  87. print_r($tax.$terms); 
  88. echo"</pre>"; 
  89. */ 
  90.  
  91. if('post' == $post_type) 
  92. $category = get_the_category(); 
  93. $ID = $category[0]->cat_ID; 
  94.  
  95. $parents = get_category_parents($ID, TRUE, '$$$', FALSE ); 
  96. $parents = explode("$$$", $parents); 
  97. foreach ($parents as $parent_item) 
  98. if($parent_item) $trail[] = $parent_item; 
  99.  
  100. if($post_type == 'portfolio') 
  101. $parents = get_the_term_list( $post_id, 'portfolio_entries', '', '$$$', '' ); 
  102. $parents = explode('$$$', $parents); 
  103. foreach ($parents as $parent_item) 
  104. if($parent_item) $trail[] = $parent_item; 
  105.  
  106.  
  107. /** If the post type path returns nothing and there is a parent, get its parents. */ 
  108. if ( empty( $path ) && 0 !== $parent || 'attachment' == $post_type ) 
  109. $trail = array_merge( $trail, onetone_breadcrumbs_get_parents( $parent, '' ) ); 
  110.  
  111. /** Toggle the display of the posts page on single blog posts. */  
  112. if ( 'post' == $post_type && $show_posts_page == true && 'page' == get_option( 'show_on_front' ) ) { 
  113. $posts_page = get_option( 'page_for_posts' ); 
  114. if ( $posts_page != '' && is_numeric( $posts_page ) ) { 
  115. $trail = array_merge( $trail, onetone_breadcrumbs_get_parents( $posts_page, '' ) ); 
  116.  
  117. /** Display terms for specific post type taxonomy if requested. */ 
  118. if ( isset( $args["singular_{$post_type}_taxonomy"] ) && $terms = get_the_term_list( $post_id, $args["singular_{$post_type}_taxonomy"], '', ', ', '' ) ) 
  119. $trail[] = $terms; 
  120.  
  121. /** End with the post title. */ 
  122. $post_title = get_the_title( $post_id ); // Force the post_id to make sure we get the correct page title. 
  123. if ( !empty( $post_title ) ) 
  124. $trail['trail_end'] = $post_title; 
  125.  
  126. /** If we're viewing any type of archive. */ 
  127. elseif ( is_archive() ) { 
  128.  
  129. /** If viewing a taxonomy term archive. */ 
  130. if ( is_tax() || is_category() || is_tag() ) { 
  131.  
  132. /** Get some taxonomy and term variables. */ 
  133. $term = $wp_query->get_queried_object(); 
  134. $taxonomy = get_taxonomy( $term->taxonomy ); 
  135.  
  136. /** Get the path to the term archive. Use this to determine if a page is present with it. */ 
  137. if ( is_category() ) 
  138. $path = get_option( 'category_base' ); 
  139. elseif ( is_tag() ) 
  140. $path = get_option( 'tag_base' ); 
  141. else { 
  142. if ( $taxonomy->rewrite['with_front'] && $wp_rewrite->front ) 
  143. $path = trailingslashit( $wp_rewrite->front ); 
  144. $path .= $taxonomy->rewrite['slug']; 
  145.  
  146. /** Get parent pages by path if they exist. */ 
  147. if ( $path ) 
  148. $trail = array_merge( $trail, onetone_breadcrumbs_get_parents( '', $path ) ); 
  149.  
  150. /** If the taxonomy is hierarchical, list its parent terms. */ 
  151. if ( is_taxonomy_hierarchical( $term->taxonomy ) && $term->parent ) 
  152. $trail = array_merge( $trail, onetone_breadcrumbs_get_term_parents( $term->parent, $term->taxonomy ) ); 
  153.  
  154. /** Add the term name to the trail end. */ 
  155. $trail['trail_end'] = $term->name; 
  156.  
  157. /** If viewing a post type archive. */ 
  158. elseif ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) { 
  159.  
  160. /** Get the post type object. */ 
  161. $post_type_object = get_post_type_object( get_query_var( 'post_type' ) ); 
  162.  
  163. /** If $front has been set, add it to the $path. */ 
  164. if ( $post_type_object->rewrite['with_front'] && $wp_rewrite->front ) 
  165. $path .= trailingslashit( $wp_rewrite->front ); 
  166.  
  167. /** If there's a slug, add it to the $path. */ 
  168. if ( !empty( $post_type_object->rewrite['archive'] ) ) 
  169. $path .= $post_type_object->rewrite['archive']; 
  170.  
  171. /** If there's a path, check for parents. */ 
  172. if ( !empty( $path ) ) 
  173. $trail = array_merge( $trail, onetone_breadcrumbs_get_parents( '', $path ) ); 
  174.  
  175. /** Add the post type [plural] name to the trail end. */ 
  176. $trail['trail_end'] = $post_type_object->labels->name; 
  177.  
  178. /** If viewing an author archive. */ 
  179. elseif ( is_author() ) { 
  180.  
  181. /** If $front has been set, add it to $path. */ 
  182. if ( !empty( $wp_rewrite->front ) ) 
  183. $path .= trailingslashit( $wp_rewrite->front ); 
  184.  
  185. /** If an $author_base exists, add it to $path. */ 
  186. if ( !empty( $wp_rewrite->author_base ) ) 
  187. $path .= $wp_rewrite->author_base; 
  188.  
  189. /** If $path exists, check for parent pages. */ 
  190. if ( !empty( $path ) ) 
  191. $trail = array_merge( $trail, onetone_breadcrumbs_get_parents( '', $path ) ); 
  192.  
  193. /** Add the author's display name to the trail end. */ 
  194. $trail['trail_end'] = get_the_author_meta( 'display_name', get_query_var( 'author' ) ); 
  195.  
  196. /** If viewing a time-based archive. */ 
  197. elseif ( is_time() ) { 
  198.  
  199. if ( get_query_var( 'minute' ) && get_query_var( 'hour' ) ) 
  200. $trail['trail_end'] = get_the_time( __( 'g:i a', 'onetone') ); 
  201.  
  202. elseif ( get_query_var( 'minute' ) ) 
  203. $trail['trail_end'] = sprintf( __( 'Minute %1$s', 'onetone'), get_the_time( __( 'i', 'onetone') ) ); 
  204.  
  205. elseif ( get_query_var( 'hour' ) ) 
  206. $trail['trail_end'] = get_the_time( __( 'g a', 'onetone') ); 
  207.  
  208. /** If viewing a date-based archive. */ 
  209. elseif ( is_date() ) { 
  210.  
  211. /** If $front has been set, check for parent pages. */ 
  212. if ( $wp_rewrite->front ) 
  213. $trail = array_merge( $trail, onetone_breadcrumbs_get_parents( '', $wp_rewrite->front ) ); 
  214.  
  215. if ( is_day() ) { 
  216. $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'onetone') ) . '">' . get_the_time( __( 'Y', 'onetone') ) . '</a>'; 
  217. $trail[] = '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '" title="' . get_the_time( esc_attr__( 'F', 'onetone') ) . '">' . get_the_time( __( 'F', 'onetone') ) . '</a>'; 
  218. $trail['trail_end'] = get_the_time( __( 'j', 'onetone') ); 
  219.  
  220. elseif ( get_query_var( 'w' ) ) { 
  221. $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'onetone') ) . '">' . get_the_time( __( 'Y', 'onetone') ) . '</a>'; 
  222. $trail['trail_end'] = sprintf( __( 'Week %1$s', 'onetone'), get_the_time( esc_attr__( 'W', 'onetone') ) ); 
  223.  
  224. elseif ( is_month() ) { 
  225. $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'onetone') ) . '">' . get_the_time( __( 'Y', 'onetone') ) . '</a>'; 
  226. $trail['trail_end'] = get_the_time( __( 'F', 'onetone') ); 
  227.  
  228. elseif ( is_year() ) { 
  229. $trail['trail_end'] = get_the_time( __( 'Y', 'onetone') ); 
  230.  
  231. /** If viewing search results. */ 
  232. elseif ( is_search() ) 
  233. $trail['trail_end'] = sprintf( __( 'Search results for "%1$s"', 'onetone'), esc_attr( get_search_query() ) ); 
  234.  
  235. /** If viewing a 404errorpage. */ 
  236. elseif ( is_404() ) 
  237. $trail['trail_end'] = __( '404 Not Found', 'onetone'); 
  238.  
  239. /** Allow child themes/plugins to filter the trail array. */ 
  240. $trail = apply_filters( 'onetone_breadcrumbs_trail', $trail, $args ); 
  241.  
  242. /** Connect the breadcrumb trail if there are items in the trail. */ 
  243. if ( is_array( $trail ) ) { 
  244.  
  245. /** Open the breadcrumb trail containers. */ 
  246. $breadcrumb = '<div class="breadcrumb breadcrumbs sillver-breadcrumbs"><div class="breadcrumb-trail">'; 
  247.  
  248. /** If $before was set, wrap it in a container. */ 
  249. if ( !empty( $before ) ) 
  250. $breadcrumb .= '<span class="trail-before">' . $before . '</span> '; 
  251.  
  252. /** Wrap the $trail['trail_end'] value in a container. */ 
  253. if ( !empty( $trail['trail_end'] ) ) 
  254. $trail['trail_end'] = '<span class="trail-end">' . $trail['trail_end'] . '</span>'; 
  255.  
  256. /** Format the separator. */ 
  257. if ( !empty( $separator ) ) 
  258. $separator = '<span class="sep">' . $separator . '</span>'; 
  259.  
  260. /** Join the individual trail items into a single string. */ 
  261. $breadcrumb .= join( " {$separator} ", $trail ); 
  262.  
  263. /** If $after was set, wrap it in a container. */ 
  264. if ( !empty( $after ) ) 
  265. $breadcrumb .= ' <span class="trail-after">' . $after . '</span>'; 
  266.  
  267. /** Close the breadcrumb trail containers. */ 
  268. $breadcrumb .= '</div></div>'; 
  269.  
  270. /** Allow developers to filter the breadcrumb trail HTML. */ 
  271. $breadcrumb = apply_filters( 'onetone_breadcrumbs', $breadcrumb ); 
  272.  
  273. /** Output the breadcrumb. */ 
  274. if ( $echo ) 
  275. echo $breadcrumb; 
  276. else 
  277. return $breadcrumb; 
  278.  
  279. } // End onetone_breadcrumbs()