Instantly share code, notes, and snippets.
Created
August 21, 2024 15:54
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save NorbertFeria/d6cb50ccf17e96430b029029b84c6686 to your computer and use it in GitHub Desktop.
Bootstrap 5 Breadcrumb for WordPress USAGE: <?php the_breadcrumbs(); ?> OR <?php echo get_the_breadcrumbs(); ?> Or through shortcode [the_breadcrumbs]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Retrieves the post title. | |
| * | |
| * @return string | |
| */ | |
| function get_the_breadcrumbs(){ | |
| $breadcrumbs_id = 'breadcrumbs'; | |
| $breadcrumbs_class = 'breadcrumb'; | |
| $homepage_title = 'Home'; | |
| global $post,$wp_query; | |
| if ( !is_front_page() ) : | |
| $breadcrumbs_str = '<nav aria-label="breadcrumb">'; | |
| $breadcrumbs_str .= '<ol id="' . $breadcrumbs_id . '" class="' . $breadcrumbs_class . '">'; | |
| // Homepage | |
| $breadcrumbs_str .= '<li class="breadcrumb-item"><a href="' . get_home_url() . '" title="' . $homepage_title . '">' . $homepage_title . '</a></li>'; | |
| if ( is_archive() && !is_tax() && !is_category() && !is_tag() ) { | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active">' . post_type_archive_title($prefix, false) . '</li>'; | |
| } else if ( is_archive() && is_tax() && !is_category() && !is_tag() ) { | |
| $post_type = get_post_type(); | |
| // If post is a custom post type | |
| if($post_type != 'post') { | |
| $post_type_object = get_post_type_object($post_type); | |
| $post_type_archive = get_post_type_archive_link($post_type); | |
| $breadcrumbs_str .= '<li class="breadcrumbs"><a title="' . $post_type_object->labels->name . '">' . $post_type_object->labels->name . '</a></li>'; | |
| } | |
| $custom_tax_name = get_queried_object()->name; | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active">' . $custom_tax_name . '</li>'; | |
| } else if ( is_single() ) { | |
| // get Post type | |
| $post_type = get_post_type(); | |
| // If custom post type | |
| if($post_type != 'post') { | |
| $post_type_object = get_post_type_object($post_type); | |
| $post_type_archive = get_post_type_archive_link($post_type); | |
| $breadcrumbs_str .= '<li class="breadcrumb-item"><a class="bread-cat bread-custom-post-type-' . $post_type . '" href="' . $post_type_archive . '" title="' . $post_type_object->labels->name . '">' . $post_type_object->labels->name . '</a></li>'; | |
| $custom_taxonomies = get_object_taxonomies( $post_type, 'objects' ); | |
| if ( ! empty( $custom_taxonomies ) && ! is_wp_error( $custom_taxonomies ) ) { | |
| $taxonomy_names = []; | |
| foreach ( $custom_taxonomies as $custom_taxonomy ) { | |
| $taxonomy_names[] = $custom_taxonomy->labels->name; | |
| } | |
| if( !empty( $taxonomy_names ) ){ | |
| foreach ( $taxonomy_names as $taxonomy_name ) { | |
| $taxonomy_terms = get_the_terms( $post->ID, $taxonomy_name ); | |
| if ( ! empty( $taxonomy_terms ) && ! is_wp_error( $taxonomy_terms ) ) { | |
| $tax_id = $taxonomy_terms[0]->term_id; | |
| $tax_slug = $taxonomy_terms[0]->slug; | |
| $tax_link = get_term_link($taxonomy_terms[0]->term_id, $taxonomy_name); | |
| $tax_name = $taxonomy_terms[0]->name; | |
| $breadcrumbs_str .= '<li class="breadcrumb-item"><a href="' . $tax_link . '">' . $tax_name . '</a></li>'; | |
| } | |
| } | |
| } | |
| } | |
| }else{ //if post | |
| $category = get_the_category(); | |
| if(!empty($category)) { | |
| // Get last category post is in | |
| $last_category = end(array_values($category)); | |
| // Get parent any categories and create array | |
| $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),','); | |
| $cat_parents = explode(',',$get_cat_parents); | |
| // Loop through parent categories and store in variable $cat_display | |
| $cat_display = ''; | |
| foreach($cat_parents as $parents) { | |
| $cat_display .= '<li class="breadcrumb-item">'.$parents.'</li>'; | |
| } | |
| } | |
| if(!empty($last_category)) { | |
| $breadcrumbs_str .= $cat_display; | |
| } | |
| } | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active" aria-current="page">' . get_the_title() . '</li>'; | |
| } else if ( is_category() ) { | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active" aria-current="page">' . single_cat_title('', false) . '</li>'; | |
| } else if ( is_page() ) { | |
| // Standard page | |
| if( $post->post_parent ){ | |
| // If child page, get parents | |
| $anc = get_post_ancestors( $post->ID ); | |
| // Get parents in the right order | |
| $anc = array_reverse($anc); | |
| // Parent page loop | |
| if ( !isset( $parents ) ) $parents = null; | |
| foreach ( $anc as $ancestor ) { | |
| $parents .= '<li class="item-parent item-parent-' . $ancestor . '"><a class="bread-parent bread-parent-' . $ancestor . '" href="' . get_permalink($ancestor) . '" title="' . get_the_title($ancestor) . '">' . get_the_title($ancestor) . '</a></li>'; | |
| } | |
| // Display parent pages | |
| $breadcrumbs_str .= $parents; | |
| // Current page | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active" aria-current="page">' . get_the_title() . '</li>'; | |
| } else { | |
| // Just display current page if no parents | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active" aria-current="page">' . get_the_title() . '</li>'; | |
| } | |
| } else if ( is_tag() ) { | |
| // Tag page | |
| // Get tag information | |
| $term_id = get_query_var('tag_id'); | |
| $taxonomy = 'post_tag'; | |
| $args = 'include=' . $term_id; | |
| $terms = get_terms( $taxonomy, $args ); | |
| $get_term_id = $terms[0]->term_id; | |
| $get_term_slug = $terms[0]->slug; | |
| $get_term_name = $terms[0]->name; | |
| // Display the tag name | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active" aria-current="page">' . $get_term_name . '</li>'; | |
| } elseif ( is_day() ) { | |
| // Day archive | |
| // Year link | |
| $breadcrumbs_str .= '<li class="breadcrumb-item item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" href="' . get_year_link( get_the_time('Y') ) . '" title="' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</a></li>'; | |
| // Month link | |
| $breadcrumbs_str .= '<li class="breadcrumb-item item-month-' . get_the_time('m') . '"><a class="bread-month bread-month-' . get_the_time('m') . '" href="' . get_month_link( get_the_time('Y'), get_the_time('m') ) . '" title="' . get_the_time('M') . '">' . get_the_time('M') . ' Archives</a></li>'; | |
| // Day display | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active item-' . get_the_time('j') . '">' . get_the_time('jS') . ' ' . get_the_time('M') . ' Archives</li>'; | |
| } else if ( is_month() ) { | |
| // Month Archive | |
| // Year link | |
| $breadcrumbs_str .= '<li class="breadcrumb-item item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" href="' . get_year_link( get_the_time('Y') ) . '" title="' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</a></li>'; | |
| // Month display | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active item-month item-month-' . get_the_time('m') . '">' . get_the_time('M') . ' Archives</li>'; | |
| } else if ( is_year() ) { | |
| // Display year archive | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active item-current item-current-' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</li>'; | |
| } else if ( is_author() ) { | |
| // Auhor archive | |
| // Get the author information | |
| global $author; | |
| $userdata = get_userdata( $author ); | |
| // Display author name | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active item-current item-current-' . $userdata->user_nicename . '">' . 'Author: ' . $userdata->display_name . '</li>'; | |
| } else if ( get_query_var('paged') ) { | |
| // Paginated archives | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active item-current item-current-' . get_query_var('paged') . '">'.__('Page') . ' ' . get_query_var('paged') . '</li>'; | |
| } else if ( is_search() ) { | |
| // Search results page | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active" aria-current="page">Search results for: ' . get_search_query() . '</li>'; | |
| } elseif ( is_404() ) { | |
| // 404 page | |
| $breadcrumbs_str .= '<li class="breadcrumb-item active" aria-current="page">' . 'Page Not Found (404)' . '</li>'; | |
| } | |
| $breadcrumbs_str .= '</ol>'; | |
| $breadcrumbs_str .= '</nav>'; | |
| endif; | |
| // Filters the breadcrumbs. | |
| return apply_filters( 'the_breadcrumbs', $breadcrumbs_str ); | |
| } | |
| /** | |
| * Displays or retrieves the current breadcrumbs with optional markup. | |
| * | |
| */ | |
| function the_breadcrumbs( $before = '', $after = '', $display = true ){ | |
| $breadcrumbs_str = get_the_breadcrumbs(); | |
| if ( strlen( $breadcrumbs_str ) === 0 ) { | |
| return; | |
| } | |
| $breadcrumbs_str = $before . $breadcrumbs_str . $after; | |
| if ( $display ) { | |
| echo $breadcrumbs_str; | |
| } else { | |
| return $breadcrumbs_str; | |
| } | |
| } | |
| /** | |
| * Function for [the_breadcrumbs] shortcode | |
| * | |
| */ | |
| function the_breadcrumbs_shortcode( $atts ) { | |
| // Extract shortcode attributes, if any | |
| $atts = shortcode_atts( [ | |
| 'before' => '', | |
| 'after' => '', | |
| ], $atts, 'the_breadcrumbs' ); | |
| return the_breadcrumbs( $atts['before'], $atts['after'], false ); | |
| } | |
| // Register the shortcode | |
| add_shortcode( 'the_breadcrumbs', 'the_breadcrumbs_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment