breadcrumb.php (8181B)
1 <?php 2 /** 3 * Appside Custom Breadcrumb 4 * @since 1.0.0 5 */ 6 7 if (!function_exists('Appside_Breadcrumb')) { 8 function Appside_Breadcrumb(){ 9 10 // Set variables for later use 11 $home_link = home_url('/'); 12 $home_text = esc_html__( 'Home','aapside' ); 13 $link_before = '<li typeof="v:Breadcrumb">'; 14 $link_after = '</li>'; 15 $link_attr = ' rel="v:url" property="v:title"'; 16 $link = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after; 17 $delimiter = ''; // Delimiter between crumbs 18 $before = '<li class="current">'; // Tag before the current crumb 19 $after = '</li>'; // Tag after the current crumb 20 $page_addon = ''; // Adds the page number if the query is paged 21 $breadcrumb_trail = ''; 22 $category_links = ''; 23 24 /** 25 * Set our own $wp_the_query variable. Do not use the global variable version due to 26 * reliability 27 */ 28 $wp_the_query = $GLOBALS['wp_the_query']; 29 $queried_object = $wp_the_query->get_queried_object(); 30 31 // Handle single post requests which includes single pages, posts and attatchments 32 if ( is_singular() ) 33 { 34 /** 35 * Set our own $post variable. Do not use the global variable version due to 36 * reliability. We will set $post_object variable to $GLOBALS['wp_the_query'] 37 */ 38 $post_object = sanitize_post( $queried_object ); 39 40 // Set variables 41 $title = $post_object->post_title ; 42 $parent = $post_object->post_parent; 43 $post_type = $post_object->post_type; 44 $post_id = $post_object->ID; 45 $post_link = $before . $title . $after; 46 $parent_string = ''; 47 $post_type_link = ''; 48 49 50 if ( 'post' === $post_type ) 51 { 52 // Get the post categories 53 $categories = get_the_category( $post_id ); 54 if ( $categories ) { 55 // Lets grab the first category 56 $category = $categories[0]; 57 58 $category_links = get_category_parents( $category, true, $delimiter ); 59 $category_links = str_replace( '<a', $link_before . '<a' . $link_attr, $category_links ); 60 $category_links = str_replace( '</a>', '</a>' . $link_after,$category_links ); 61 } 62 } 63 64 if ( !in_array( $post_type, ['post', 'page', 'attachment'] ) ) 65 { 66 $post_type_object = get_post_type_object( $post_type ); 67 $archive_link = esc_url( get_post_type_archive_link( $post_type ) ); 68 69 $post_type_link = sprintf( $link, $archive_link, $post_type_object->labels->singular_name ); 70 } 71 72 // Get post parents if $parent !== 0 73 if ( 0 !== $parent ) 74 { 75 $parent_links = []; 76 while ( $parent ) { 77 $post_parent = get_post( $parent ); 78 79 $parent_links[] = sprintf( $link, esc_url( get_permalink( $post_parent->ID ) ), get_the_title( $post_parent->ID ) ); 80 81 $parent = $post_parent->post_parent; 82 } 83 84 $parent_links = array_reverse( $parent_links ); 85 86 $parent_string = implode( $delimiter, $parent_links ); 87 } 88 89 // Lets build the breadcrumb trail 90 if ( $parent_string ) { 91 $breadcrumb_trail = $parent_string . $delimiter . $post_link; 92 } else { 93 $breadcrumb_trail = $post_link; 94 } 95 96 if ( $post_type_link ) 97 $breadcrumb_trail = $post_type_link . $delimiter . $breadcrumb_trail; 98 99 if ( $category_links ) 100 $breadcrumb_trail = $category_links . $breadcrumb_trail; 101 } 102 103 // Handle archives which includes category-, tag-, taxonomy-, date-, custom post type archives and author archives 104 if( is_archive() ) 105 { 106 if ( is_category() 107 || is_tag() 108 || is_tax() 109 ) { 110 // Set the variables for this section 111 $term_object = get_term( $queried_object ); 112 $taxonomy = $term_object->taxonomy; 113 $term_id = $term_object->term_id; 114 $term_name = $term_object->name; 115 $term_parent = $term_object->parent; 116 $taxonomy_object = get_taxonomy( $taxonomy ); 117 $current_term_link = $before . $taxonomy_object->labels->singular_name . ': ' . $term_name . $after; 118 $parent_term_string = ''; 119 120 if ( 0 !== $term_parent ) 121 { 122 // Get all the current term ancestors 123 $parent_term_links = []; 124 while ( $term_parent ) { 125 $term = get_term( $term_parent, $taxonomy ); 126 127 $parent_term_links[] = sprintf( $link, esc_url( get_term_link( $term ) ), $term->name ); 128 129 $term_parent = $term->parent; 130 } 131 132 $parent_term_links = array_reverse( $parent_term_links ); 133 $parent_term_string = implode( $delimiter, $parent_term_links ); 134 } 135 136 if ( $parent_term_string ) { 137 $breadcrumb_trail = $parent_term_string . $delimiter . $current_term_link; 138 } else { 139 $breadcrumb_trail = $current_term_link; 140 } 141 142 } elseif ( is_author() ) { 143 144 $breadcrumb_trail = $before . esc_html__( 'Author archive for: ','aapside') . $queried_object->data->display_name . $after; 145 146 } elseif ( is_date() ) { 147 // Set default variables 148 $get_query_var = get_query_var('m'); 149 $year = $wp_the_query->query_vars['year']; 150 $monthnum = $wp_the_query->query_vars['monthnum']; 151 $day = $wp_the_query->query_vars['day']; 152 if ( 0 == $year || 0 == $monthnum ){ 153 $year = substr($get_query_var ,0,4); 154 $monthnum = substr($get_query_var ,4,2);; 155 } 156 157 // Get the month name if $monthnum has a value 158 if ( $monthnum ) { 159 $date_time = DateTime::createFromFormat( '!m', $monthnum ); 160 $month_name = $date_time->format( 'F' ); 161 } 162 163 if ( is_year() ) { 164 $breadcrumb_trail = $before . $year . $after; 165 166 } elseif( is_month() ) { 167 $year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year ); 168 169 $breadcrumb_trail = $year_link . $delimiter . $before . $month_name . $after; 170 171 } elseif( is_day() ) { 172 173 $year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year ); 174 $month_link = sprintf( $link, esc_url( get_month_link( $year, $monthnum ) ), $month_name ); 175 176 $breadcrumb_trail = $year_link . $delimiter . $month_link . $delimiter . $before . $day . $after; 177 } 178 179 } elseif ( is_post_type_archive() ) { 180 181 $post_type = $wp_the_query->query_vars['post_type']; 182 $post_type_object = get_post_type_object( $post_type ); 183 184 $breadcrumb_trail = $before . $post_type_object->labels->singular_name . $after; 185 186 } 187 } 188 189 // Handle the search page 190 if ( is_search() ) { 191 $breadcrumb_trail = $before . esc_html__( 'Searched for: ','aapside' ) . get_search_query() . $after; 192 } 193 194 // Handle 404's 195 if ( is_404() ) { 196 $breadcrumb_trail = $before . esc_html__( 'Error 404','aapside' ) . $after; 197 } 198 199 // Handle paged pages 200 if ( is_paged() ) { 201 $current_page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' ); 202 $page_addon = $before . sprintf( esc_html__( ' ( Page %s )' ,'aapside'), number_format_i18n( $current_page ) ) . $after; 203 } 204 205 $breadcrumb_output_link = ''; 206 $breadcrumb_output_link .= '<ul class="page-list">'; 207 if ( is_home() 208 || is_front_page() 209 ) { 210 // Do not show breadcrumbs on page one of home and frontpage 211 if ( is_paged() ) { 212 $breadcrumb_output_link .= '<li><a href="' .esc_url( $home_link ). '">' . esc_html($home_text ). '</a></li>'; 213 $breadcrumb_output_link .= $page_addon; 214 } 215 if (is_home() && !is_front_page()){ 216 $breadcrumb_output_link .= '<li><a href="' . esc_url($home_link ). '">' . esc_html($home_text) . '</a></li>'; 217 $breadcrumb_output_link .= '<li> '. esc_html(get_the_title(get_option( 'page_for_posts' ))). '</li>'; 218 } 219 } 220 else { 221 $breadcrumb_output_link .= '<li><a href="' . esc_url($home_link) . '" rel="v:url" property="v:title">' . esc_html($home_text) . '</a></li>'; 222 $breadcrumb_output_link .= $delimiter; 223 $breadcrumb_output_link .= $breadcrumb_trail; 224 $breadcrumb_output_link .= $page_addon; 225 } 226 $breadcrumb_output_link .= '</ul><!-- .breadcrumbs -->'; 227 echo wp_kses_post($breadcrumb_output_link); 228 } 229 230 }