Created
May 1, 2026 11:16
-
-
Save hmowais/b96e4dfe026d35a286120dfe387c4990 to your computer and use it in GitHub Desktop.
Updated [Bread Crumbs Shortcode - 2026]
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 | |
| /** | |
| * Breadcrumb Shortcode for WordPress | |
| * Paste into your theme's functions.php | |
| * | |
| * Usage in page/post editor: [breadcrumb] | |
| * Custom options: [breadcrumb home="HOME" sep="»" class="my-bc"] | |
| */ | |
| if ( ! function_exists( 'custom_breadcrumb_shortcode' ) ) : | |
| add_shortcode( 'breadcrumb', 'custom_breadcrumb_shortcode' ); | |
| function custom_breadcrumb_shortcode( $atts ) { | |
| $atts = shortcode_atts([ | |
| 'home' => 'HOME', | |
| 'sep' => '»', | |
| 'class' => 'breadcrumb-nav', | |
| ], $atts, 'breadcrumb' ); | |
| $items = []; | |
| $items[] = [ 'label' => strtoupper( $atts['home'] ), 'url' => home_url('/'), 'active' => false ]; | |
| if ( is_singular() ) { | |
| $post_type = get_post_type(); | |
| if ( 'page' === $post_type ) { | |
| foreach ( array_reverse( get_post_ancestors( get_the_ID() ) ) as $pid ) { | |
| $items[] = [ 'label' => strtoupper( get_the_title( $pid ) ), 'url' => get_permalink( $pid ), 'active' => false ]; | |
| } | |
| } | |
| if ( 'post' === $post_type ) { | |
| $cats = get_the_category(); | |
| if ( $cats ) { | |
| $chain = []; | |
| $c = $cats[0]; | |
| while ( $c ) { array_unshift( $chain, $c ); $c = $c->parent ? get_category( $c->parent ) : null; } | |
| foreach ( $chain as $c ) { | |
| $items[] = [ 'label' => strtoupper( $c->name ), 'url' => get_category_link( $c->term_id ), 'active' => false ]; | |
| } | |
| } | |
| } | |
| if ( 'post' !== $post_type && 'page' !== $post_type ) { | |
| $pto = get_post_type_object( $post_type ); | |
| if ( $pto && $pto->has_archive ) { | |
| $items[] = [ 'label' => strtoupper( $pto->labels->name ), 'url' => get_post_type_archive_link( $post_type ), 'active' => false ]; | |
| } | |
| } | |
| $items[] = [ 'label' => strtoupper( get_the_title() ), 'url' => '', 'active' => true ]; | |
| } elseif ( is_category() ) { | |
| $cat = get_queried_object(); | |
| foreach ( array_reverse( get_ancestors( $cat->term_id, 'category' ) ) as $aid ) { | |
| $a = get_category( $aid ); | |
| $items[] = [ 'label' => strtoupper( $a->name ), 'url' => get_category_link( $aid ), 'active' => false ]; | |
| } | |
| $items[] = [ 'label' => strtoupper( $cat->name ), 'url' => '', 'active' => true ]; | |
| } elseif ( is_tax() ) { | |
| $term = get_queried_object(); | |
| $items[] = [ 'label' => strtoupper( get_taxonomy( $term->taxonomy )->labels->name ), 'url' => '', 'active' => false ]; | |
| $items[] = [ 'label' => strtoupper( $term->name ), 'url' => '', 'active' => true ]; | |
| } elseif ( is_post_type_archive() ) { | |
| $items[] = [ 'label' => strtoupper( post_type_archive_title( '', false ) ), 'url' => '', 'active' => true ]; | |
| } elseif ( is_tag() ) { | |
| $items[] = [ 'label' => strtoupper( single_tag_title( '', false ) ), 'url' => '', 'active' => true ]; | |
| } elseif ( is_author() ) { | |
| $items[] = [ 'label' => 'AUTHOR: ' . strtoupper( get_the_author() ), 'url' => '', 'active' => true ]; | |
| } elseif ( is_date() ) { | |
| $items[] = [ 'label' => strtoupper( get_the_date('F Y') ), 'url' => '', 'active' => true ]; | |
| } elseif ( is_search() ) { | |
| $items[] = [ 'label' => 'SEARCH: ' . strtoupper( get_search_query() ), 'url' => '', 'active' => true ]; | |
| } elseif ( is_404() ) { | |
| $items[] = [ 'label' => '404 NOT FOUND', 'url' => '', 'active' => true ]; | |
| } | |
| $sep = '<span class="bc-sep">' . esc_html( $atts['sep'] ) . '</span>'; | |
| $html = '<nav class="' . esc_attr( $atts['class'] ) . '" aria-label="Breadcrumb">'; | |
| $html .= '<ol>'; | |
| $total = count( $items ); | |
| foreach ( $items as $i => $item ) { | |
| $is_last = ( $i === $total - 1 ); | |
| $html .= '<li class="bc-item">'; | |
| if ( ! $item['active'] && ! empty( $item['url'] ) ) { | |
| $html .= '<a class="bc-link" href="' . esc_url( $item['url'] ) . '">' . esc_html( $item['label'] ) . '</a>'; | |
| } else { | |
| $html .= '<span class="bc-link active" aria-current="page">' . esc_html( $item['label'] ) . '</span>'; | |
| } | |
| if ( ! $is_last ) $html .= $sep; | |
| $html .= '</li>'; | |
| } | |
| $html .= '</ol></nav>'; | |
| return $html; | |
| } | |
| endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment