bbp_get_breadcrumb

Return a breadcrumb ( forum -> topic -> reply ).

Description

(string) bbp_get_breadcrumb( (array) $args = array() ); 

Returns (string)

Breadcrumbs

Parameters (1)

0. $args — Optional. (array) => array()
Separator. Defaults to ←

Usage

  1. if ( !function_exists( 'bbp_get_breadcrumb' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/common/template.php'; 
  3.  
  4. // Separator. Defaults to '←' 
  5. $args = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_get_breadcrumb($args); 
  9.  

Defined (1)

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

/includes/common/template.php  
  1. function bbp_get_breadcrumb( $args = array() ) { 
  2.  
  3. // Turn off breadcrumbs 
  4. if ( apply_filters( 'bbp_no_breadcrumb', is_front_page() ) ) 
  5. return; 
  6.  
  7. // Define variables 
  8. $front_id = $root_id = 0; 
  9. $ancestors = $crumbs = $tag_data = array(); 
  10. $pre_root_text = $pre_front_text = $pre_current_text = ''; 
  11. $pre_include_root = $pre_include_home = $pre_include_current = true; 
  12.  
  13. /** Home Text *********************************************************/ 
  14.  
  15. // No custom home text 
  16. if ( empty( $args['home_text'] ) ) { 
  17.  
  18. $front_id = get_option( 'page_on_front' ); 
  19.  
  20. // Set home text to page title 
  21. if ( !empty( $front_id ) ) { 
  22. $pre_front_text = get_the_title( $front_id ); 
  23.  
  24. // Default to 'Home' 
  25. } else { 
  26. $pre_front_text = __( 'Home', bbpress ); 
  27.  
  28. /** Root Text *********************************************************/ 
  29.  
  30. // No custom root text 
  31. if ( empty( $args['root_text'] ) ) { 
  32. if ( !empty( $page ) ) { 
  33. $root_id = $page->ID; 
  34. $pre_root_text = bbp_get_forum_archive_title(); 
  35.  
  36. /** Includes **********************************************************/ 
  37.  
  38. // Root slug is also the front page 
  39. if ( !empty( $front_id ) && ( $front_id === $root_id ) ) { 
  40. $pre_include_root = false; 
  41.  
  42. // Don't show root if viewing forum archive 
  43. if ( bbp_is_forum_archive() ) { 
  44. $pre_include_root = false; 
  45.  
  46. // Don't show root if viewing page in place of forum archive 
  47. if ( !empty( $root_id ) && ( ( is_single() || is_page() ) && ( $root_id === get_the_ID() ) ) ) { 
  48. $pre_include_root = false; 
  49.  
  50. /** Current Text ******************************************************/ 
  51.  
  52. // Search page 
  53. if ( bbp_is_search() ) { 
  54. $pre_current_text = bbp_get_search_title(); 
  55.  
  56. // Forum archive 
  57. } elseif ( bbp_is_forum_archive() ) { 
  58. $pre_current_text = bbp_get_forum_archive_title(); 
  59.  
  60. // Topic archive 
  61. } elseif ( bbp_is_topic_archive() ) { 
  62. $pre_current_text = bbp_get_topic_archive_title(); 
  63.  
  64. // View 
  65. } elseif ( bbp_is_single_view() ) { 
  66. $pre_current_text = bbp_get_view_title(); 
  67.  
  68. // Single Forum 
  69. } elseif ( bbp_is_single_forum() ) { 
  70. $pre_current_text = bbp_get_forum_title(); 
  71.  
  72. // Single Topic 
  73. } elseif ( bbp_is_single_topic() ) { 
  74. $pre_current_text = bbp_get_topic_title(); 
  75.  
  76. // Single Topic 
  77. } elseif ( bbp_is_single_reply() ) { 
  78. $pre_current_text = bbp_get_reply_title(); 
  79.  
  80. // Topic Tag (or theme compat topic tag) 
  81. } elseif ( bbp_is_topic_tag() || ( get_query_var( 'bbp_topic_tag' ) && !bbp_is_topic_tag_edit() ) ) { 
  82.  
  83. // Always include the tag name 
  84. $tag_data[] = bbp_get_topic_tag_name(); 
  85.  
  86. // If capable, include a link to edit the tag 
  87. if ( current_user_can( 'manage_topic_tags' ) ) { 
  88. $tag_data[] = '<a href="' . esc_url( bbp_get_topic_tag_edit_link() ) . '" class="bbp-edit-topic-tag-link">' . esc_html__( '(Edit)', bbpress ) . '</a>'; 
  89.  
  90. // Implode the results of the tag data 
  91. $pre_current_text = sprintf( __( 'Topic Tag: %s', bbpress ), implode( ' ', $tag_data ) ); 
  92.  
  93. // Edit Topic Tag 
  94. } elseif ( bbp_is_topic_tag_edit() ) { 
  95. $pre_current_text = __( 'Edit', bbpress ); 
  96.  
  97. // Single 
  98. } else { 
  99. $pre_current_text = get_the_title(); 
  100.  
  101. /** Parse Args ********************************************************/ 
  102.  
  103. // Parse args 
  104. $r = bbp_parse_args( $args, array( 
  105.  
  106. // HTML 
  107. 'before' => '<div class="bbp-breadcrumb"><p>',  
  108. 'after' => '</p></div>',  
  109.  
  110. // Separator 
  111. 'sep' => is_rtl() ? __( '‹', bbpress ) : __( '›', bbpress ),  
  112. 'pad_sep' => 1,  
  113. 'sep_before' => '<span class="bbp-breadcrumb-sep">',  
  114. 'sep_after' => '</span>',  
  115.  
  116. // Crumbs 
  117. 'crumb_before' => '',  
  118. 'crumb_after' => '',  
  119.  
  120. // Home 
  121. 'include_home' => $pre_include_home,  
  122. 'home_text' => $pre_front_text,  
  123.  
  124. // Forum root 
  125. 'include_root' => $pre_include_root,  
  126. 'root_text' => $pre_root_text,  
  127.  
  128. // Current 
  129. 'include_current' => $pre_include_current,  
  130. 'current_text' => $pre_current_text,  
  131. 'current_before' => '<span class="bbp-breadcrumb-current">',  
  132. 'current_after' => '</span>',  
  133. ), 'get_breadcrumb' ); 
  134.  
  135. /** Ancestors *********************************************************/ 
  136.  
  137. // Get post ancestors 
  138. if ( is_singular() || bbp_is_forum_edit() || bbp_is_topic_edit() || bbp_is_reply_edit() ) { 
  139. $ancestors = array_reverse( (array) get_post_ancestors( get_the_ID() ) ); 
  140.  
  141. // Do we want to include a link to home? 
  142. if ( !empty( $r['include_home'] ) || empty( $r['home_text'] ) ) { 
  143. $crumbs[] = '<a href="' . trailingslashit( home_url() ) . '" class="bbp-breadcrumb-home">' . $r['home_text'] . '</a>'; 
  144.  
  145. // Do we want to include a link to the forum root? 
  146. if ( !empty( $r['include_root'] ) || empty( $r['root_text'] ) ) { 
  147.  
  148. // Page exists at root slug path, so use its permalink 
  149. if ( !empty( $page ) ) { 
  150. $root_url = get_permalink( $page->ID ); 
  151.  
  152. // Use the root slug 
  153. } else { 
  154. $root_url = get_post_type_archive_link( bbp_get_forum_post_type() ); 
  155.  
  156. // Add the breadcrumb 
  157. $crumbs[] = '<a href="' . esc_url( $root_url ) . '" class="bbp-breadcrumb-root">' . $r['root_text'] . '</a>'; 
  158.  
  159. // Ancestors exist 
  160. if ( !empty( $ancestors ) ) { 
  161.  
  162. // Loop through parents 
  163. foreach ( (array) $ancestors as $parent_id ) { 
  164.  
  165. // Parents 
  166. $parent = get_post( $parent_id ); 
  167.  
  168. // Skip parent if empty or error 
  169. if ( empty( $parent ) || is_wp_error( $parent ) ) 
  170. continue; 
  171.  
  172. // Switch through post_type to ensure correct filters are applied 
  173. switch ( $parent->post_type ) { 
  174.  
  175. // Forum 
  176. $crumbs[] = '<a href="' . esc_url( bbp_get_forum_permalink( $parent->ID ) ) . '" class="bbp-breadcrumb-forum">' . bbp_get_forum_title( $parent->ID ) . '</a>'; 
  177. break; 
  178.  
  179. // Topic 
  180. $crumbs[] = '<a href="' . esc_url( bbp_get_topic_permalink( $parent->ID ) ) . '" class="bbp-breadcrumb-topic">' . bbp_get_topic_title( $parent->ID ) . '</a>'; 
  181. break; 
  182.  
  183. // Reply (Note: not in most themes) 
  184. $crumbs[] = '<a href="' . esc_url( bbp_get_reply_permalink( $parent->ID ) ) . '" class="bbp-breadcrumb-reply">' . bbp_get_reply_title( $parent->ID ) . '</a>'; 
  185. break; 
  186.  
  187. // WordPress Post/Page/Other 
  188. default : 
  189. $crumbs[] = '<a href="' . esc_url( get_permalink( $parent->ID ) ) . '" class="bbp-breadcrumb-item">' . get_the_title( $parent->ID ) . '</a>'; 
  190. break; 
  191.  
  192. // Edit topic tag 
  193. } elseif ( bbp_is_topic_tag_edit() ) { 
  194. $crumbs[] = '<a href="' . esc_url( get_term_link( bbp_get_topic_tag_id(), bbp_get_topic_tag_tax_id() ) ) . '" class="bbp-breadcrumb-topic-tag">' . sprintf( __( 'Topic Tag: %s', bbpress ), bbp_get_topic_tag_name() ) . '</a>'; 
  195.  
  196. // Search 
  197. } elseif ( bbp_is_search() && bbp_get_search_terms() ) { 
  198. $crumbs[] = '<a href="' . esc_url( bbp_get_search_url() ) . '" class="bbp-breadcrumb-search">' . esc_html__( 'Search', bbpress ) . '</a>'; 
  199.  
  200. /** Current ***********************************************************/ 
  201.  
  202. // Add current page to breadcrumb 
  203. if ( !empty( $r['include_current'] ) || empty( $r['current_text'] ) ) { 
  204. $crumbs[] = $r['current_before'] . $r['current_text'] . $r['current_after']; 
  205.  
  206. /** Separator *********************************************************/ 
  207.  
  208. // Wrap the separator in before/after before padding and filter 
  209. if ( ! empty( $r['sep'] ) ) { 
  210. $sep = $r['sep_before'] . $r['sep'] . $r['sep_after']; 
  211.  
  212. // Pad the separator 
  213. if ( !empty( $r['pad_sep'] ) ) { 
  214. if ( function_exists( 'mb_strlen' ) ) { 
  215. $sep = str_pad( $sep, mb_strlen( $sep ) + ( (int) $r['pad_sep'] * 2 ), ' ', STR_PAD_BOTH ); 
  216. } else { 
  217. $sep = str_pad( $sep, strlen( $sep ) + ( (int) $r['pad_sep'] * 2 ), ' ', STR_PAD_BOTH ); 
  218.  
  219. /** Finish Up *********************************************************/ 
  220.  
  221. // Filter the separator and breadcrumb 
  222. $sep = apply_filters( 'bbp_breadcrumb_separator', $sep ); 
  223. $crumbs = apply_filters( 'bbp_breadcrumbs', $crumbs ); 
  224.  
  225. // Build the trail 
  226. $trail = !empty( $crumbs ) ? ( $r['before'] . $r['crumb_before'] . implode( $sep . $r['crumb_after'] . $r['crumb_before'] , $crumbs ) . $r['crumb_after'] . $r['after'] ) : ''; 
  227.  
  228. return apply_filters( 'bbp_get_breadcrumb', $trail, $crumbs, $r );