breadcrumbs.php

I’ll create a strong breadcrumbs.php template file for your RX theme with SEO schema, WooCommerce support, CPT/taxonomy/archive/search/404 handling, accessibility, filters, and safe escaping.

Yes my dear friend — create this file:

rx-theme/inc/breadcrumbs.php
or
rx-theme/template-parts/breadcrumbs.php

Then paste this full advanced code.

<?php
/**
 * RX Theme Advanced Breadcrumbs
 *
 * File: breadcrumbs.php
 * Usage:
 * 1. Include this file from functions.php:
 *    require_once get_template_directory() . '/inc/breadcrumbs.php';
 *
 * 2. Show breadcrumb in theme template:
 *    <?php rx_theme_breadcrumbs(); ?>
 *
 * Features:
 * - Home breadcrumb
 * - Single post breadcrumb
 * - Page parent hierarchy
 * - Category, tag, taxonomy archive
 * - Custom post type archive
 * - Custom taxonomy support
 * - Author archive
 * - Date archive
 * - Search result breadcrumb
 * - 404 breadcrumb
 * - WooCommerce support
 * - Blog page support
 * - Attachment support
 * - Breadcrumb schema JSON-LD
 * - Accessible navigation
 * - Filter hooks
 * - Safe escaping
 * - Theme CSS helper
 *
 * @package RX_Theme
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Main breadcrumb output function.
 *
 * Example:
 * rx_theme_breadcrumbs();
 */
if ( ! function_exists( 'rx_theme_breadcrumbs' ) ) {

	function rx_theme_breadcrumbs( $args = array() ) {

		if ( is_front_page() ) {
			return;
		}

		$defaults = array(
			'container'          => 'nav',
			'container_class'    => 'rx-breadcrumbs',
			'list_class'         => 'rx-breadcrumbs__list',
			'item_class'         => 'rx-breadcrumbs__item',
			'link_class'         => 'rx-breadcrumbs__link',
			'current_class'      => 'rx-breadcrumbs__current',
			'separator'          => '<span class="rx-breadcrumbs__separator" aria-hidden="true">/</span>',
			'home_text'          => esc_html__( 'Home', 'rx-theme' ),
			'blog_text'          => esc_html__( 'Blog', 'rx-theme' ),
			'search_text'        => esc_html__( 'Search results for:', 'rx-theme' ),
			'error_text'         => esc_html__( '404 Not Found', 'rx-theme' ),
			'author_text'        => esc_html__( 'Author:', 'rx-theme' ),
			'date_text'          => esc_html__( 'Archives:', 'rx-theme' ),
			'show_home'          => true,
			'show_current'       => true,
			'show_on_home'       => false,
			'show_schema'        => true,
			'show_separator'     => true,
			'aria_label'         => esc_attr__( 'Breadcrumb navigation', 'rx-theme' ),
			'before'             => '',
			'after'              => '',
			'max_title_length'   => 90,
		);

		$args = wp_parse_args( $args, $defaults );

		/**
		 * Filter breadcrumb arguments.
		 */
		$args = apply_filters( 'rx_theme_breadcrumbs_args', $args );

		if ( is_front_page() && empty( $args['show_on_home'] ) ) {
			return;
		}

		$items = rx_theme_get_breadcrumb_items( $args );

		if ( empty( $items ) || ! is_array( $items ) ) {
			return;
		}

		$items = apply_filters( 'rx_theme_breadcrumb_items', $items, $args );

		if ( empty( $items ) ) {
			return;
		}

		echo wp_kses_post( $args['before'] );

		$container = tag_escape( $args['container'] );

		printf(
			'<%1$s class="%2$s" aria-label="%3$s">',
			$container,
			esc_attr( $args['container_class'] ),
			esc_attr( $args['aria_label'] )
		);

		echo '<ol class="' . esc_attr( $args['list_class'] ) . '" itemscope itemtype="https://schema.org/BreadcrumbList">';

		$total = count( $items );
		$count = 0;

		foreach ( $items as $item ) {
			$count++;

			$title      = isset( $item['title'] ) ? $item['title'] : '';
			$url        = isset( $item['url'] ) ? $item['url'] : '';
			$is_current = ( $count === $total );

			if ( empty( $title ) ) {
				continue;
			}

			$title = rx_theme_trim_breadcrumb_title( $title, $args['max_title_length'] );

			echo '<li class="' . esc_attr( $args['item_class'] ) . '" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">';

			if ( ! empty( $url ) && ! $is_current ) {
				printf(
					'<a class="%1$s" href="%2$s" itemprop="item"><span itemprop="name">%3$s</span></a>',
					esc_attr( $args['link_class'] ),
					esc_url( $url ),
					esc_html( $title )
				);
			} else {
				if ( ! empty( $args['show_current'] ) ) {
					printf(
						'<span class="%1$s" itemprop="name" aria-current="page">%2$s</span>',
						esc_attr( $args['current_class'] ),
						esc_html( $title )
					);
				}
			}

			printf(
				'<meta itemprop="position" content="%d" />',
				absint( $count )
			);

			if ( ! $is_current && ! empty( $args['show_separator'] ) ) {
				echo wp_kses_post( $args['separator'] );
			}

			echo '</li>';
		}

		echo '</ol>';
		printf( '</%s>', $container );

		if ( ! empty( $args['show_schema'] ) ) {
			rx_theme_breadcrumb_schema( $items );
		}

		echo wp_kses_post( $args['after'] );
	}
}

/**
 * Build breadcrumb item array.
 */
if ( ! function_exists( 'rx_theme_get_breadcrumb_items' ) ) {

	function rx_theme_get_breadcrumb_items( $args = array() ) {

		$items = array();

		if ( ! empty( $args['show_home'] ) ) {
			$items[] = array(
				'title' => $args['home_text'],
				'url'   => home_url( '/' ),
			);
		}

		// WooCommerce shop/page support.
		if ( rx_theme_is_woocommerce_active() ) {
			if ( is_shop() ) {
				$shop_id = wc_get_page_id( 'shop' );

				if ( $shop_id && $shop_id > 0 ) {
					$items[] = array(
						'title' => get_the_title( $shop_id ),
						'url'   => '',
					);
				}

				return $items;
			}

			if ( is_product_category() || is_product_tag() || is_product_taxonomy() ) {
				$items = rx_theme_add_woocommerce_shop_item( $items );

				$term = get_queried_object();

				if ( $term && ! is_wp_error( $term ) ) {
					$items = rx_theme_add_term_parents( $items, $term );
					$items[] = array(
						'title' => single_term_title( '', false ),
						'url'   => '',
					);
				}

				return $items;
			}

			if ( is_product() ) {
				$items = rx_theme_add_woocommerce_shop_item( $items );

				$product_terms = get_the_terms( get_the_ID(), 'product_cat' );

				if ( ! empty( $product_terms ) && ! is_wp_error( $product_terms ) ) {
					$main_term = rx_theme_get_deepest_term( $product_terms );
					$items     = rx_theme_add_term_parents( $items, $main_term );

					$items[] = array(
						'title' => $main_term->name,
						'url'   => get_term_link( $main_term ),
					);
				}

				$items[] = array(
					'title' => get_the_title(),
					'url'   => '',
				);

				return $items;
			}
		}

		// Blog page support for posts.
		if ( is_singular( 'post' ) || is_category() || is_tag() || is_date() || is_author() ) {
			$items = rx_theme_add_blog_page_item( $items, $args );
		}

		if ( is_home() ) {
			$items[] = array(
				'title' => rx_theme_get_blog_page_title( $args ),
				'url'   => '',
			);

			return $items;
		}

		if ( is_singular() ) {
			$post = get_queried_object();

			if ( $post instanceof WP_Post ) {
				$post_type = get_post_type( $post );

				if ( 'page' === $post_type ) {
					$items = rx_theme_add_page_parents( $items, $post );
				} elseif ( 'post' === $post_type ) {
					$items = rx_theme_add_post_taxonomy_items( $items, $post );
				} elseif ( 'attachment' === $post_type ) {
					$items = rx_theme_add_attachment_items( $items, $post );
				} else {
					$items = rx_theme_add_post_type_archive_item( $items, $post_type );
					$items = rx_theme_add_custom_post_taxonomy_items( $items, $post );
				}

				$items[] = array(
					'title' => get_the_title( $post ),
					'url'   => '',
				);
			}

			return $items;
		}

		if ( is_post_type_archive() ) {
			$post_type = get_query_var( 'post_type' );

			if ( is_array( $post_type ) ) {
				$post_type = reset( $post_type );
			}

			$obj = get_post_type_object( $post_type );

			if ( $obj ) {
				$items[] = array(
					'title' => $obj->labels->name,
					'url'   => '',
				);
			}

			return $items;
		}

		if ( is_category() ) {
			$term = get_queried_object();

			if ( $term && ! is_wp_error( $term ) ) {
				$items = rx_theme_add_term_parents( $items, $term );

				$items[] = array(
					'title' => single_cat_title( '', false ),
					'url'   => '',
				);
			}

			return $items;
		}

		if ( is_tag() ) {
			$items[] = array(
				'title' => single_tag_title( '', false ),
				'url'   => '',
			);

			return $items;
		}

		if ( is_tax() ) {
			$term = get_queried_object();

			if ( $term && ! is_wp_error( $term ) ) {
				$taxonomy = get_taxonomy( $term->taxonomy );

				if ( $taxonomy && ! empty( $taxonomy->object_type ) ) {
					$post_type = reset( $taxonomy->object_type );
					$items     = rx_theme_add_post_type_archive_item( $items, $post_type );
				}

				$items = rx_theme_add_term_parents( $items, $term );

				$items[] = array(
					'title' => single_term_title( '', false ),
					'url'   => '',
				);
			}

			return $items;
		}

		if ( is_author() ) {
			$author = get_queried_object();

			$author_name = '';

			if ( $author && isset( $author->display_name ) ) {
				$author_name = $author->display_name;
			}

			$items[] = array(
				'title' => trim( $args['author_text'] . ' ' . $author_name ),
				'url'   => '',
			);

			return $items;
		}

		if ( is_search() ) {
			$items[] = array(
				'title' => $args['search_text'] . ' ' . get_search_query(),
				'url'   => '',
			);

			return $items;
		}

		if ( is_date() ) {
			$items = rx_theme_add_date_archive_items( $items, $args );
			return $items;
		}

		if ( is_404() ) {
			$items[] = array(
				'title' => $args['error_text'],
				'url'   => '',
			);

			return $items;
		}

		if ( is_archive() ) {
			$items[] = array(
				'title' => get_the_archive_title(),
				'url'   => '',
			);

			return $items;
		}

		return $items;
	}
}

/**
 * Add blog page item.
 */
if ( ! function_exists( 'rx_theme_add_blog_page_item' ) ) {

	function rx_theme_add_blog_page_item( $items, $args = array() ) {

		$page_for_posts = (int) get_option( 'page_for_posts' );

		if ( $page_for_posts > 0 && 'page' === get_post_type( $page_for_posts ) ) {
			$items[] = array(
				'title' => get_the_title( $page_for_posts ),
				'url'   => get_permalink( $page_for_posts ),
			);
		} elseif ( ! empty( $args['blog_text'] ) ) {
			$items[] = array(
				'title' => $args['blog_text'],
				'url'   => home_url( '/' ),
			);
		}

		return $items;
	}
}

/**
 * Get blog page title.
 */
if ( ! function_exists( 'rx_theme_get_blog_page_title' ) ) {

	function rx_theme_get_blog_page_title( $args = array() ) {

		$page_for_posts = (int) get_option( 'page_for_posts' );

		if ( $page_for_posts > 0 ) {
			return get_the_title( $page_for_posts );
		}

		return isset( $args['blog_text'] ) ? $args['blog_text'] : esc_html__( 'Blog', 'rx-theme' );
	}
}

/**
 * Add page parent hierarchy.
 */
if ( ! function_exists( 'rx_theme_add_page_parents' ) ) {

	function rx_theme_add_page_parents( $items, $post ) {

		if ( ! $post instanceof WP_Post ) {
			return $items;
		}

		$parents = get_post_ancestors( $post );

		if ( ! empty( $parents ) ) {
			$parents = array_reverse( $parents );

			foreach ( $parents as $parent_id ) {
				$items[] = array(
					'title' => get_the_title( $parent_id ),
					'url'   => get_permalink( $parent_id ),
				);
			}
		}

		return $items;
	}
}

/**
 * Add post category hierarchy.
 */
if ( ! function_exists( 'rx_theme_add_post_taxonomy_items' ) ) {

	function rx_theme_add_post_taxonomy_items( $items, $post ) {

		if ( ! $post instanceof WP_Post ) {
			return $items;
		}

		$primary_category = rx_theme_get_primary_category( $post->ID );

		if ( $primary_category && ! is_wp_error( $primary_category ) ) {
			$items = rx_theme_add_term_parents( $items, $primary_category );

			$items[] = array(
				'title' => $primary_category->name,
				'url'   => get_term_link( $primary_category ),
			);
		}

		return $items;
	}
}

/**
 * Get primary category.
 * Supports Yoast primary category if available, otherwise deepest category.
 */
if ( ! function_exists( 'rx_theme_get_primary_category' ) ) {

	function rx_theme_get_primary_category( $post_id ) {

		$post_id = absint( $post_id );

		if ( ! $post_id ) {
			return false;
		}

		// Yoast SEO primary category support.
		$yoast_primary = get_post_meta( $post_id, '_yoast_wpseo_primary_category', true );

		if ( ! empty( $yoast_primary ) ) {
			$term = get_term( absint( $yoast_primary ), 'category' );

			if ( $term && ! is_wp_error( $term ) ) {
				return $term;
			}
		}

		$categories = get_the_category( $post_id );

		if ( empty( $categories ) || is_wp_error( $categories ) ) {
			return false;
		}

		return rx_theme_get_deepest_term( $categories );
	}
}

/**
 * Add taxonomy parents.
 */
if ( ! function_exists( 'rx_theme_add_term_parents' ) ) {

	function rx_theme_add_term_parents( $items, $term ) {

		if ( ! $term instanceof WP_Term ) {
			return $items;
		}

		if ( empty( $term->parent ) ) {
			return $items;
		}

		$parents = get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' );

		if ( empty( $parents ) ) {
			return $items;
		}

		$parents = array_reverse( $parents );

		foreach ( $parents as $parent_id ) {
			$parent = get_term( $parent_id, $term->taxonomy );

			if ( $parent && ! is_wp_error( $parent ) ) {
				$items[] = array(
					'title' => $parent->name,
					'url'   => get_term_link( $parent ),
				);
			}
		}

		return $items;
	}
}

/**
 * Add custom post type archive item.
 */
if ( ! function_exists( 'rx_theme_add_post_type_archive_item' ) ) {

	function rx_theme_add_post_type_archive_item( $items, $post_type ) {

		if ( empty( $post_type ) || 'post' === $post_type || 'page' === $post_type || 'attachment' === $post_type ) {
			return $items;
		}

		$obj = get_post_type_object( $post_type );

		if ( ! $obj ) {
			return $items;
		}

		if ( ! empty( $obj->has_archive ) ) {
			$archive_link = get_post_type_archive_link( $post_type );

			if ( $archive_link ) {
				$items[] = array(
					'title' => $obj->labels->name,
					'url'   => $archive_link,
				);
			}
		}

		return $items;
	}
}

/**
 * Add custom post type taxonomy items.
 */
if ( ! function_exists( 'rx_theme_add_custom_post_taxonomy_items' ) ) {

	function rx_theme_add_custom_post_taxonomy_items( $items, $post ) {

		if ( ! $post instanceof WP_Post ) {
			return $items;
		}

		$post_type  = get_post_type( $post );
		$taxonomies = get_object_taxonomies( $post_type, 'objects' );

		if ( empty( $taxonomies ) ) {
			return $items;
		}

		$preferred_taxonomies = apply_filters(
			'rx_theme_breadcrumb_preferred_taxonomies',
			array(
				'category',
				'product_cat',
				'portfolio_category',
				'project_category',
				'service_category',
				'course_category',
				'knowledge_base_category',
			),
			$post_type
		);

		$selected_taxonomy = '';

		foreach ( $preferred_taxonomies as $taxonomy_name ) {
			if ( isset( $taxonomies[ $taxonomy_name ] ) ) {
				$selected_taxonomy = $taxonomy_name;
				break;
			}
		}

		if ( empty( $selected_taxonomy ) ) {
			foreach ( $taxonomies as $taxonomy ) {
				if ( ! empty( $taxonomy->hierarchical ) ) {
					$selected_taxonomy = $taxonomy->name;
					break;
				}
			}
		}

		if ( empty( $selected_taxonomy ) ) {
			return $items;
		}

		$terms = get_the_terms( $post->ID, $selected_taxonomy );

		if ( empty( $terms ) || is_wp_error( $terms ) ) {
			return $items;
		}

		$term = rx_theme_get_deepest_term( $terms );

		if ( $term && ! is_wp_error( $term ) ) {
			$items = rx_theme_add_term_parents( $items, $term );

			$items[] = array(
				'title' => $term->name,
				'url'   => get_term_link( $term ),
			);
		}

		return $items;
	}
}

/**
 * Add attachment breadcrumb.
 */
if ( ! function_exists( 'rx_theme_add_attachment_items' ) ) {

	function rx_theme_add_attachment_items( $items, $post ) {

		if ( ! $post instanceof WP_Post ) {
			return $items;
		}

		if ( ! empty( $post->post_parent ) ) {
			$parent = get_post( $post->post_parent );

			if ( $parent ) {
				$items[] = array(
					'title' => get_the_title( $parent ),
					'url'   => get_permalink( $parent ),
				);
			}
		}

		return $items;
	}
}

/**
 * Date archive breadcrumb items.
 */
if ( ! function_exists( 'rx_theme_add_date_archive_items' ) ) {

	function rx_theme_add_date_archive_items( $items, $args = array() ) {

		$year  = get_query_var( 'year' );
		$month = get_query_var( 'monthnum' );
		$day   = get_query_var( 'day' );

		if ( is_year() ) {
			$items[] = array(
				'title' => sprintf(
					'%s %s',
					isset( $args['date_text'] ) ? $args['date_text'] : esc_html__( 'Archives:', 'rx-theme' ),
					get_the_date( 'Y' )
				),
				'url'   => '',
			);

			return $items;
		}

		if ( is_month() ) {
			$items[] = array(
				'title' => get_the_date( 'Y' ),
				'url'   => get_year_link( $year ),
			);

			$items[] = array(
				'title' => get_the_date( 'F' ),
				'url'   => '',
			);

			return $items;
		}

		if ( is_day() ) {
			$items[] = array(
				'title' => get_the_date( 'Y' ),
				'url'   => get_year_link( $year ),
			);

			$items[] = array(
				'title' => get_the_date( 'F' ),
				'url'   => get_month_link( $year, $month ),
			);

			$items[] = array(
				'title' => get_the_date( 'j' ),
				'url'   => '',
			);

			return $items;
		}

		$items[] = array(
			'title' => get_the_archive_title(),
			'url'   => '',
		);

		return $items;
	}
}

/**
 * WooCommerce active check.
 */
if ( ! function_exists( 'rx_theme_is_woocommerce_active' ) ) {

	function rx_theme_is_woocommerce_active() {
		return class_exists( 'WooCommerce' );
	}
}

/**
 * Add WooCommerce shop item.
 */
if ( ! function_exists( 'rx_theme_add_woocommerce_shop_item' ) ) {

	function rx_theme_add_woocommerce_shop_item( $items ) {

		if ( ! rx_theme_is_woocommerce_active() ) {
			return $items;
		}

		$shop_id = wc_get_page_id( 'shop' );

		if ( $shop_id && $shop_id > 0 ) {
			$items[] = array(
				'title' => get_the_title( $shop_id ),
				'url'   => get_permalink( $shop_id ),
			);
		}

		return $items;
	}
}

/**
 * Find deepest term from terms list.
 */
if ( ! function_exists( 'rx_theme_get_deepest_term' ) ) {

	function rx_theme_get_deepest_term( $terms ) {

		if ( empty( $terms ) || ! is_array( $terms ) ) {
			return false;
		}

		$deepest_term  = reset( $terms );
		$max_depth     = -1;

		foreach ( $terms as $term ) {
			if ( ! $term instanceof WP_Term ) {
				continue;
			}

			$ancestors = get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' );
			$depth     = is_array( $ancestors ) ? count( $ancestors ) : 0;

			if ( $depth > $max_depth ) {
				$max_depth    = $depth;
				$deepest_term = $term;
			}
		}

		return $deepest_term;
	}
}

/**
 * Trim long breadcrumb title.
 */
if ( ! function_exists( 'rx_theme_trim_breadcrumb_title' ) ) {

	function rx_theme_trim_breadcrumb_title( $title, $max_length = 90 ) {

		$title      = wp_strip_all_tags( $title );
		$max_length = absint( $max_length );

		if ( $max_length <= 0 ) {
			return $title;
		}

		if ( function_exists( 'mb_strlen' ) && function_exists( 'mb_substr' ) ) {
			if ( mb_strlen( $title ) > $max_length ) {
				return mb_substr( $title, 0, $max_length ) . '...';
			}
		} else {
			if ( strlen( $title ) > $max_length ) {
				return substr( $title, 0, $max_length ) . '...';
			}
		}

		return $title;
	}
}

/**
 * JSON-LD breadcrumb schema.
 */
if ( ! function_exists( 'rx_theme_breadcrumb_schema' ) ) {

	function rx_theme_breadcrumb_schema( $items ) {

		if ( empty( $items ) || ! is_array( $items ) ) {
			return;
		}

		$schema_items = array();
		$position     = 1;

		foreach ( $items as $item ) {
			if ( empty( $item['title'] ) ) {
				continue;
			}

			$url = ! empty( $item['url'] ) ? $item['url'] : rx_theme_get_current_url();

			$schema_items[] = array(
				'@type'    => 'ListItem',
				'position' => $position,
				'name'     => wp_strip_all_tags( $item['title'] ),
				'item'     => esc_url_raw( $url ),
			);

			$position++;
		}

		if ( empty( $schema_items ) ) {
			return;
		}

		$schema = array(
			'@context'        => 'https://schema.org',
			'@type'           => 'BreadcrumbList',
			'itemListElement' => $schema_items,
		);

		echo '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . '</script>';
	}
}

/**
 * Get current URL safely.
 */
if ( ! function_exists( 'rx_theme_get_current_url' ) ) {

	function rx_theme_get_current_url() {

		global $wp;

		if ( isset( $wp->request ) ) {
			return home_url( add_query_arg( array(), $wp->request ) );
		}

		return home_url( '/' );
	}
}

/**
 * Optional shortcode:
 * [rx_breadcrumbs]
 */
if ( ! function_exists( 'rx_theme_breadcrumbs_shortcode' ) ) {

	function rx_theme_breadcrumbs_shortcode( $atts ) {

		ob_start();

		rx_theme_breadcrumbs();

		return ob_get_clean();
	}

	add_shortcode( 'rx_breadcrumbs', 'rx_theme_breadcrumbs_shortcode' );
}

/**
 * Optional Gutenberg block-like reusable function.
 */
if ( ! function_exists( 'rx_theme_has_breadcrumbs' ) ) {

	function rx_theme_has_breadcrumbs() {
		return ! is_front_page();
	}
}

/**
 * Optional body class when breadcrumb can appear.
 */
if ( ! function_exists( 'rx_theme_breadcrumb_body_class' ) ) {

	function rx_theme_breadcrumb_body_class( $classes ) {

		if ( rx_theme_has_breadcrumbs() ) {
			$classes[] = 'has-rx-breadcrumbs';
		}

		return $classes;
	}

	add_filter( 'body_class', 'rx_theme_breadcrumb_body_class' );
}

/**
 * Optional inline CSS.
 * Better method: move this CSS into style.css.
 */
if ( ! function_exists( 'rx_theme_breadcrumb_inline_css' ) ) {

	function rx_theme_breadcrumb_inline_css() {

		$css = '
		.rx-breadcrumbs {
			width: 100%;
			margin: 0 0 20px;
			font-size: 14px;
			line-height: 1.6;
		}

		.rx-breadcrumbs__list {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			gap: 6px;
			margin: 0;
			padding: 0;
			list-style: none;
		}

		.rx-breadcrumbs__item {
			display: inline-flex;
			align-items: center;
			gap: 6px;
			margin: 0;
			padding: 0;
			color: inherit;
		}

		.rx-breadcrumbs__link {
			color: inherit;
			text-decoration: none;
		}

		.rx-breadcrumbs__link:hover,
		.rx-breadcrumbs__link:focus {
			text-decoration: underline;
		}

		.rx-breadcrumbs__current {
			font-weight: 600;
			opacity: 0.85;
		}

		.rx-breadcrumbs__separator {
			opacity: 0.55;
		}

		@media (max-width: 600px) {
			.rx-breadcrumbs {
				font-size: 13px;
			}

			.rx-breadcrumbs__list {
				gap: 4px;
			}
		}
		';

		$css = apply_filters( 'rx_theme_breadcrumb_inline_css', $css );

		if ( empty( $css ) ) {
			return;
		}

		echo '<style id="rx-theme-breadcrumb-css">' . wp_strip_all_tags( $css ) . '</style>';
	}

	add_action( 'wp_head', 'rx_theme_breadcrumb_inline_css', 20 );
}

Now add this line in your functions.php:

require_once get_template_directory() . '/inc/breadcrumbs.php';

Then show breadcrumb in your theme template, usually inside header.php, single.php, page.php, or archive.php:

<?php
if ( function_exists( 'rx_theme_breadcrumbs' ) ) {
	rx_theme_breadcrumbs();
}
?>

Best place for most themes:

<main id="primary" class="site-main">

	<?php
	if ( function_exists( 'rx_theme_breadcrumbs' ) ) {
		rx_theme_breadcrumbs();
	}
	?>

	<!-- Page content here -->

</main>

You can also use shortcode inside editor:

[rx_breadcrumbs]

And you can customize from functions.php like this:

add_filter( 'rx_theme_breadcrumbs_args', function( $args ) {

	$args['home_text']        = 'RX Home';
	$args['blog_text']        = 'Health Blog';
	$args['separator']        = '<span class="rx-breadcrumbs__separator" aria-hidden="true"></span>';
	$args['max_title_length'] = 70;

	return $args;
} );

This file is safe for a normal WordPress theme and supports future expansion for posts, pages, taxonomies, custom post types, WooCommerce products, search, author, archive, date, and 404 pages.

Leave a Reply

Your email address will not be published. Required fields are marked *