Advertisement
Guest User

breadcrumb.php

a guest
Jan 30th, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. function audioman_breadcrumb() {
  2. $show_on_home = get_theme_mod( 'audioman_breadcrumb_on_homepage' );
  3. $delimiter = get_theme_mod( 'audioman_breadcrumb_seperator', '>' );
  4.  
  5. if ( $delimiter ) {
  6. $delimiter = '<span class="sep">' . $delimiter . '</span>';
  7. }
  8.  
  9. /* === OPTIONS === */
  10. $text['home'] = esc_html__( 'Home', 'audioman-pro' ); // text for the 'Home' link
  11. /* translators: 1: before text/html, 2: after text/html. */
  12. $text['category'] = esc_html__( '%1$s Archive for %2$s', 'audioman-pro' ); // text for a category page
  13. /* translators: 1: before text/html, 2: after text/html. */
  14. $text['search'] = esc_html__( '%1$sSearch results for: %2$s', 'audioman-pro' ); // text for a search results page
  15. /* translators: 1: before text/html, 2: after text/html. */
  16. $text['tag'] = esc_html__( '%1$sPosts tagged %2$s', 'audioman-pro' ); // text for a tag page
  17. /* translators: 1: before text/html, 2: after text/html. */
  18. $text['author'] = esc_html__( '%1$sView all posts by %2$s', 'audioman-pro' ); // text for an author page
  19. $text['404'] = esc_html__( 'Error 404', 'audioman-pro' ); // text for the 404 page
  20.  
  21.  
  22. $showCurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
  23. $before = '<span class="breadcrumb-current">'; // tag before the current crumb
  24. $after = '</span>'; // tag after the current crumb
  25. /* === END OF OPTIONS === */
  26.  
  27. global $post, $paged, $page;
  28. $homeLink = home_url( '/' );
  29. $linkBefore = '<span class="breadcrumb">';
  30. $linkAfter = '</span>';
  31. $linkAttr = '';
  32. $link = $linkBefore . '<a' . $linkAttr . ' href="%1$s">%2$s</a>' . wp_kses_post( $delimiter ) . $linkAfter;
  33.  
  34. global $post, $paged, $page;
  35. $homeLink = home_url( '/' );
  36. $linkBefore = '<span class="breadcrumb">';
  37. $linkAfter = '</span>';
  38. $linkAttr = '';
  39. $link = $linkBefore . '<a' . $linkAttr . ' href="%1$s">%2$s</a>' . wp_kses_post( $delimiter ) . $linkAfter;
  40.  
  41. if ( is_front_page() ) {
  42. if ( $show_on_home ) {
  43. echo '
  44. <div class="breadcrumb-area custom">
  45. <nav class="entry-breadcrumbs">';
  46.  
  47. echo $linkBefore . '<a href="' . esc_url( $homeLink ) . '">' . $text['home'] . '</a>' . $linkAfter; // WPCS: XSS OK.
  48.  
  49. echo '</nav><!-- .entry-breadcrumbs -->
  50. </div><!-- .breadcrumb-area -->';
  51. }
  52. } else {
  53. echo '<div class="breadcrumb-area custom">
  54. <nav class="entry-breadcrumbs">';
  55.  
  56. echo sprintf( $link, esc_url( $homeLink ), $text['home'] ); // WPCS: XSS OK.
  57.  
  58. if ( is_home() ) {
  59. echo $before . esc_html( get_the_title( get_option( 'page_for_posts', true ) ) ) . $after; // WPCS: XSS OK.
  60. } elseif ( is_category() ) {
  61. $thisCat = get_category( get_query_var( 'cat' ), false );
  62.  
  63. if ( $thisCat->parent != 0 ) {
  64. $cats = get_category_parents( $thisCat->parent, true, false );
  65. $cats = str_replace( '<a', $linkBefore . '<a' . $linkAttr, $cats );
  66. $cats = str_replace( '</a>', '</a>' . wp_kses_post( $delimiter ) . $linkAfter, $cats );
  67. echo $cats; // WPCS: XSS OK.
  68. }
  69.  
  70. the_archive_title( $before . sprintf( $text['category'], '<span class="archive-text">', '</span>' ), $after );
  71.  
  72. } elseif ( is_search() ) {
  73. echo $before . sprintf( $text['search'], '<span class="search-text">', '</span>' . get_search_query() ) . $after; // WPCS: XSS OK.
  74.  
  75. } elseif ( is_day() ) {
  76. echo sprintf( $link, esc_url( get_year_link( get_the_time( __( 'Y', 'audioman-pro' ) ) ) ), esc_html( get_the_time( __( 'Y', 'audioman-pro' ) ) ) ) ; // WPCS: XSS OK.
  77.  
  78.  
  79. echo sprintf( $link, esc_url( get_month_link( get_the_time( __( 'Y', 'audioman-pro' ) ) ) ), esc_html(get_the_time( __( 'F', 'audioman-pro' ) ) ) ); // WPCS: XSS OK.
  80.  
  81. echo $before . esc_html( get_the_time( __( 'd', 'audioman-pro' ) ) ) . $after; // WPCS: XSS OK.
  82.  
  83. } elseif ( is_month() ) {
  84. echo sprintf( $link, esc_url( get_year_link( get_the_time( __( 'Y', 'audioman-pro' ) ) ) ), esc_html( get_the_time( __( 'Y', 'audioman-pro' ) ) ) ) ; // WPCS: XSS OK.
  85.  
  86. echo $before . esc_html( get_the_time( __( 'F', 'audioman-pro' ) ) ) . $after; // WPCS: XSS OK.
  87.  
  88.  
  89. } elseif ( is_year() ) {
  90. echo $before . esc_html( get_the_time( __( 'Y', 'audioman-pro' ) ) ) . $after; // WPCS: XSS OK.
  91.  
  92.  
  93. } elseif ( is_single() && !is_attachment() ) {
  94. if ( get_post_type() != 'post' ) {
  95. $post_type = get_post_type_object( get_post_type() );
  96. $post_link = get_post_type_archive_link( $post_type->name );
  97.  
  98. if( 'jetpack-portfolio' == get_post_type() ) {
  99. printf( $link, esc_url( $post_link ), esc_html( '', 'audioman-pro' ) ); // WPCS: XSS OK.
  100. } else {
  101. printf( $link, esc_url( $post_link ), esc_html( $post_type->labels->singular_name ) ); // WPCS: XSS OK.
  102. }
  103.  
  104.  
  105.  
  106. echo $before . esc_html( get_the_title() ) . $after; // WPCS: XSS OK.
  107. }
  108. else {
  109. $cat = get_the_category();
  110. $cat = $cat[0];
  111. $cats = get_category_parents( $cat, true, '' );
  112. $cats = preg_replace( "#^(.+)$#", "$1", $cats );
  113. $cats = str_replace( '<a', $linkBefore . '<a' . $linkAttr, $cats );
  114. $cats = str_replace( '</a>', '</a>' . wp_kses_post( $delimiter ) . $linkAfter, $cats );
  115. echo $cats; // WPCS: XSS OK.
  116.  
  117. echo $before . esc_html( get_the_title() ) . $after; // WPCS: XSS OK.
  118. }
  119. } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
  120. $post_type = get_post_type_object( get_post_type() );
  121. echo isset( $post_type->labels->singular_name ) ? $before . esc_html( $post_type->labels->singular_name ) . $after : ''; // WPCS: XSS OK.
  122. } elseif ( is_attachment() ) {
  123. $parent = get_post( $post->post_parent );
  124. $cat = get_the_category( $parent->ID );
  125.  
  126. if ( isset( $cat[0] ) ) {
  127. $cat = $cat[0];
  128. }
  129.  
  130. if ( $cat ) {
  131. $cats = get_category_parents( $cat, true );
  132. $cats = str_replace( '<a', $linkBefore . '<a' . $linkAttr, $cats );
  133. $cats = str_replace( '</a>', '</a>' . wp_kses_post( $delimiter ) . $linkAfter, $cats );
  134. echo $cats; // WPCS: XSS OK.
  135. }
  136.  
  137. printf( $link, esc_url( get_permalink( $parent ) ), esc_html( $parent->post_title ) ); // WPCS: XSS OK.
  138.  
  139. echo $before . esc_html( get_the_title() ) . $after; // WPCS: XSS OK.
  140. } elseif ( is_page() && ! $post->post_parent ) {
  141. echo $before . esc_html( get_the_title() ) . $after; // WPCS: XSS OK.
  142. } elseif ( is_page() && $post->post_parent ) {
  143. $parent_id = $post->post_parent;
  144. $breadcrumbs = array();
  145.  
  146. while( $parent_id ) {
  147. $page_child = get_post( $parent_id );
  148. $breadcrumbs[] = sprintf( $link, esc_url( get_permalink( $page_child->ID ) ), esc_html( get_the_title( $page_child->ID ) ) );
  149. $parent_id = $page_child->post_parent;
  150. }
  151.  
  152. $breadcrumbs = array_reverse( $breadcrumbs );
  153.  
  154. for( $i = 0; $i < count( $breadcrumbs ); $i++ ) {
  155. echo $breadcrumbs[$i]; // WPCS: XSS OK.
  156. }
  157.  
  158. echo $before . esc_html( get_the_title() ) . $after; // WPCS: XSS OK.
  159. } elseif ( is_tag() ) {
  160. the_archive_title( $before . sprintf( $text['tag'], '<span class="tag-text">', '</span>' ), $after );
  161.  
  162. } elseif ( is_author() ) {
  163. global $author;
  164. $userdata = get_userdata( $author );
  165. echo $before . sprintf( $text['author'], '<span class="author-text">', '</span>' . $userdata->display_name ) . $after; // WPCS: XSS OK.
  166.  
  167. } elseif ( is_404() ) {
  168. echo $before . $text['404'] . $after; // WPCS: XSS OK.
  169.  
  170. }
  171.  
  172. if ( get_query_var( 'paged' ) ) {
  173. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
  174. echo ' (';
  175. }
  176.  
  177. /* translators: %s: current page number. */
  178. echo sprintf( esc_html__( 'Page %s', 'audioman-pro' ), absint( max( $paged, $page ) ) );
  179.  
  180. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
  181. echo ')';
  182. }
  183. }
  184.  
  185. echo '</nav><!-- .entry-breadcrumbs -->
  186. </div><!-- .breadcrumb-area -->';
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement