breadcrumbs.php

I’ll make this as a full advanced breadcrumbs.php component for your RX Theme, with schema, accessibility, WooCommerce/support checks, custom post types, taxonomies, pagination, search, 404, attachments, and safe escaping.

Below is a very advanced template-parts/components/breadcrumbs.php file for your RX Theme.

Create this file:

rx-theme/
└── template-parts/
    └── components/
        └── breadcrumbs.php

Then paste this full code inside breadcrumbs.php.

<?php
/**
 * RX Theme Advanced Breadcrumbs Component
 *
 * File: template-parts/components/breadcrumbs.php
 *
 * Usage:
 * get_template_part( 'template-parts/components/breadcrumbs' );
 *
 * Features:
 * - Accessible breadcrumb navigation
 * - Schema.org BreadcrumbList JSON-LD
 * - Home breadcrumb
 * - Blog page breadcrumb
 * - Category, tag, taxonomy breadcrumbs
 * - Single post breadcrumbs
 * - Custom post type archive breadcrumbs
 * - Custom taxonomy breadcrumbs
 * - Page parent hierarchy breadcrumbs
 * - Attachment breadcrumbs
 * - Author archive breadcrumbs
 * - Search result breadcrumbs
 * - Date archive breadcrumbs
 * - 404 breadcrumbs
 * - WooCommerce support
 * - bbPress support
 * - Pagination support
 * - Query var support
 * - RTL friendly markup
 * - Safe escaping and sanitization
 *
 * @package RX_Theme
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'rx_theme_get_breadcrumbs_items' ) ) {

	/**
	 * Build breadcrumb items.
	 *
	 * @return array
	 */
	function rx_theme_get_breadcrumbs_items() {

		$items = array();

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

		/**
		 * Home item.
		 */
		$items[] = array(
			'label' => rx_theme_breadcrumb_home_label(),
			'url'   => home_url( '/' ),
		);

		/**
		 * WooCommerce shop/page support.
		 */
		if ( function_exists( 'is_woocommerce' ) && rx_theme_is_woocommerce_breadcrumb_context() ) {

			$shop_page_id = function_exists( 'wc_get_page_id' ) ? wc_get_page_id( 'shop' ) : 0;

			if ( $shop_page_id > 0 && ! is_shop() ) {
				$items[] = array(
					'label' => get_the_title( $shop_page_id ),
					'url'   => get_permalink( $shop_page_id ),
				);
			}

			if ( is_shop() ) {
				$items[] = array(
					'label' => $shop_page_id > 0 ? get_the_title( $shop_page_id ) : esc_html__( 'Shop', 'rx-theme' ),
					'url'   => '',
				);
			}

			if ( is_product_category() || is_product_tag() ) {
				$term = get_queried_object();

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

			if ( is_product() ) {
				global $post;

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

				if ( ! empty( $product_terms ) && ! is_wp_error( $product_terms ) ) {
					$primary_term = rx_theme_get_deepest_term( $product_terms );
					if ( $primary_term ) {
						$items = rx_theme_add_term_ancestors_to_breadcrumbs( $items, $primary_term );
						$items[] = array(
							'label' => $primary_term->name,
							'url'   => get_term_link( $primary_term ),
						);
					}
				}

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

			if ( is_cart() ) {
				$items[] = array(
					'label' => esc_html__( 'Cart', 'rx-theme' ),
					'url'   => '',
				);
			}

			if ( is_checkout() ) {
				$items[] = array(
					'label' => esc_html__( 'Checkout', 'rx-theme' ),
					'url'   => '',
				);
			}

			if ( is_account_page() ) {
				$items[] = array(
					'label' => esc_html__( 'My Account', 'rx-theme' ),
					'url'   => '',
				);
			}

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * bbPress support.
		 */
		if ( function_exists( 'is_bbpress' ) && is_bbpress() ) {

			if ( function_exists( 'bbp_get_forums_url' ) ) {
				$items[] = array(
					'label' => esc_html__( 'Forums', 'rx-theme' ),
					'url'   => bbp_get_forums_url(),
				);
			}

			if ( function_exists( 'bbp_is_single_forum' ) && bbp_is_single_forum() ) {
				$items[] = array(
					'label' => function_exists( 'bbp_get_forum_title' ) ? bbp_get_forum_title() : get_the_title(),
					'url'   => '',
				);
			}

			if ( function_exists( 'bbp_is_single_topic' ) && bbp_is_single_topic() ) {
				$forum_id = function_exists( 'bbp_get_topic_forum_id' ) ? bbp_get_topic_forum_id() : 0;

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

				$items[] = array(
					'label' => function_exists( 'bbp_get_topic_title' ) ? bbp_get_topic_title() : get_the_title(),
					'url'   => '',
				);
			}

			if ( function_exists( 'bbp_is_single_reply' ) && bbp_is_single_reply() ) {
				$items[] = array(
					'label' => esc_html__( 'Reply', 'rx-theme' ),
					'url'   => '',
				);
			}

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Front page.
		 */
		if ( is_front_page() ) {
			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Blog posts index page.
		 */
		if ( is_home() ) {
			if ( 'page' === $show_on_front && $page_for_posts ) {
				$items[] = array(
					'label' => get_the_title( $page_for_posts ),
					'url'   => '',
				);
			} else {
				$items[] = array(
					'label' => esc_html__( 'Blog', 'rx-theme' ),
					'url'   => '',
				);
			}

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Singular views.
		 */
		if ( is_singular() ) {

			global $post;

			$post_id   = get_the_ID();
			$post_type = get_post_type( $post_id );

			/**
			 * Attachments.
			 */
			if ( is_attachment() ) {

				$parent_id = wp_get_post_parent_id( $post_id );

				if ( $parent_id ) {
					$parent_post_type = get_post_type( $parent_id );

					if ( 'post' === $parent_post_type ) {
						$items = rx_theme_add_blog_page_to_breadcrumbs( $items );
						$items = rx_theme_add_post_categories_to_breadcrumbs( $items, $parent_id );
					} elseif ( 'page' === $parent_post_type ) {
						$items = rx_theme_add_page_ancestors_to_breadcrumbs( $items, $parent_id );
					} else {
						$items = rx_theme_add_post_type_archive_to_breadcrumbs( $items, $parent_post_type );
					}

					$items[] = array(
						'label' => get_the_title( $parent_id ),
						'url'   => get_permalink( $parent_id ),
					);
				}

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

				return rx_theme_filter_breadcrumb_items( $items );
			}

			/**
			 * Posts.
			 */
			if ( 'post' === $post_type ) {
				$items = rx_theme_add_blog_page_to_breadcrumbs( $items );
				$items = rx_theme_add_post_categories_to_breadcrumbs( $items, $post_id );

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

				return rx_theme_filter_breadcrumb_items( $items );
			}

			/**
			 * Pages.
			 */
			if ( 'page' === $post_type ) {
				$items = rx_theme_add_page_ancestors_to_breadcrumbs( $items, $post_id );

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

				return rx_theme_filter_breadcrumb_items( $items );
			}

			/**
			 * Custom post types.
			 */
			$items = rx_theme_add_post_type_archive_to_breadcrumbs( $items, $post_type );

			/**
			 * Custom post type taxonomy support.
			 */
			$taxonomies = get_object_taxonomies( $post_type, 'objects' );

			if ( ! empty( $taxonomies ) ) {
				foreach ( $taxonomies as $taxonomy ) {
					if ( empty( $taxonomy->public ) ) {
						continue;
					}

					$terms = get_the_terms( $post_id, $taxonomy->name );

					if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
						$primary_term = rx_theme_get_deepest_term( $terms );

						if ( $primary_term ) {
							$items = rx_theme_add_term_ancestors_to_breadcrumbs( $items, $primary_term );

							$term_link = get_term_link( $primary_term );
							if ( ! is_wp_error( $term_link ) ) {
								$items[] = array(
									'label' => $primary_term->name,
									'url'   => $term_link,
								);
							}
						}

						break;
					}
				}
			}

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

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Category archives.
		 */
		if ( is_category() ) {
			$term = get_queried_object();

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

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

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Tag archives.
		 */
		if ( is_tag() ) {
			$items = rx_theme_add_blog_page_to_breadcrumbs( $items );

			$items[] = array(
				'label' => sprintf(
					/* translators: %s: tag name */
					esc_html__( 'Tag: %s', 'rx-theme' ),
					single_tag_title( '', false )
				),
				'url'   => '',
			);

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Custom taxonomy archives.
		 */
		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_to_breadcrumbs( $items, $post_type );
				}

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

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

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Post type archive.
		 */
		if ( is_post_type_archive() ) {
			$post_type = get_query_var( 'post_type' );

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

			$post_type_object = get_post_type_object( $post_type );

			$items[] = array(
				'label' => $post_type_object && ! empty( $post_type_object->labels->name )
					? $post_type_object->labels->name
					: post_type_archive_title( '', false ),
				'url'   => '',
			);

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Author archive.
		 */
		if ( is_author() ) {
			$author = get_queried_object();

			$items = rx_theme_add_blog_page_to_breadcrumbs( $items );

			$items[] = array(
				'label' => sprintf(
					/* translators: %s: author name */
					esc_html__( 'Author: %s', 'rx-theme' ),
					$author && ! empty( $author->display_name ) ? $author->display_name : esc_html__( 'Author', 'rx-theme' )
				),
				'url'   => '',
			);

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Search results.
		 */
		if ( is_search() ) {
			$items[] = array(
				'label' => sprintf(
					/* translators: %s: search query */
					esc_html__( 'Search results for: %s', 'rx-theme' ),
					get_search_query()
				),
				'url'   => '',
			);

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Date archives.
		 */
		if ( is_date() ) {
			$items = rx_theme_add_blog_page_to_breadcrumbs( $items );

			if ( is_year() ) {
				$items[] = array(
					'label' => get_the_date( _x( 'Y', 'yearly archives date format', 'rx-theme' ) ),
					'url'   => '',
				);
			} elseif ( is_month() ) {
				$year = get_the_date( _x( 'Y', 'yearly archives date format', 'rx-theme' ) );

				$items[] = array(
					'label' => $year,
					'url'   => get_year_link( get_the_date( 'Y' ) ),
				);

				$items[] = array(
					'label' => get_the_date( _x( 'F', 'monthly archives date format', 'rx-theme' ) ),
					'url'   => '',
				);
			} elseif ( is_day() ) {
				$year  = get_the_date( 'Y' );
				$month = get_the_date( 'm' );

				$items[] = array(
					'label' => get_the_date( _x( 'Y', 'yearly archives date format', 'rx-theme' ) ),
					'url'   => get_year_link( $year ),
				);

				$items[] = array(
					'label' => get_the_date( _x( 'F', 'monthly archives date format', 'rx-theme' ) ),
					'url'   => get_month_link( $year, $month ),
				);

				$items[] = array(
					'label' => get_the_date( _x( 'j', 'daily archives date format', 'rx-theme' ) ),
					'url'   => '',
				);
			}

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * 404 page.
		 */
		if ( is_404() ) {
			$items[] = array(
				'label' => esc_html__( '404 Not Found', 'rx-theme' ),
				'url'   => '',
			);

			return rx_theme_filter_breadcrumb_items( $items );
		}

		/**
		 * Fallback archive.
		 */
		if ( is_archive() ) {
			$items[] = array(
				'label' => get_the_archive_title(),
				'url'   => '',
			);

			return rx_theme_filter_breadcrumb_items( $items );
		}

		return rx_theme_filter_breadcrumb_items( $items );
	}
}

if ( ! function_exists( 'rx_theme_breadcrumb_home_label' ) ) {

	/**
	 * Home label.
	 *
	 * @return string
	 */
	function rx_theme_breadcrumb_home_label() {
		return apply_filters( 'rx_theme_breadcrumb_home_label', esc_html__( 'Home', 'rx-theme' ) );
	}
}

if ( ! function_exists( 'rx_theme_is_woocommerce_breadcrumb_context' ) ) {

	/**
	 * Check WooCommerce breadcrumb context.
	 *
	 * @return bool
	 */
	function rx_theme_is_woocommerce_breadcrumb_context() {
		return (
			function_exists( 'is_woocommerce' ) && is_woocommerce()
		) || (
			function_exists( 'is_cart' ) && is_cart()
		) || (
			function_exists( 'is_checkout' ) && is_checkout()
		) || (
			function_exists( 'is_account_page' ) && is_account_page()
		);
	}
}

if ( ! function_exists( 'rx_theme_add_blog_page_to_breadcrumbs' ) ) {

	/**
	 * Add blog page to breadcrumb items.
	 *
	 * @param array $items Breadcrumb items.
	 * @return array
	 */
	function rx_theme_add_blog_page_to_breadcrumbs( $items ) {

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

		if ( 'page' === $show_on_front && $page_for_posts && ! is_home() ) {
			$items[] = array(
				'label' => get_the_title( $page_for_posts ),
				'url'   => get_permalink( $page_for_posts ),
			);
		}

		return $items;
	}
}

if ( ! function_exists( 'rx_theme_add_post_type_archive_to_breadcrumbs' ) ) {

	/**
	 * Add custom post type archive link.
	 *
	 * @param array  $items     Breadcrumb items.
	 * @param string $post_type Post type.
	 * @return array
	 */
	function rx_theme_add_post_type_archive_to_breadcrumbs( $items, $post_type ) {

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

		$post_type_object = get_post_type_object( $post_type );

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

		$archive_link = get_post_type_archive_link( $post_type );

		if ( $archive_link ) {
			$items[] = array(
				'label' => ! empty( $post_type_object->labels->name ) ? $post_type_object->labels->name : $post_type,
				'url'   => $archive_link,
			);
		}

		return $items;
	}
}

if ( ! function_exists( 'rx_theme_add_page_ancestors_to_breadcrumbs' ) ) {

	/**
	 * Add parent pages.
	 *
	 * @param array $items   Breadcrumb items.
	 * @param int   $post_id Post ID.
	 * @return array
	 */
	function rx_theme_add_page_ancestors_to_breadcrumbs( $items, $post_id ) {

		$ancestors = get_post_ancestors( $post_id );

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

			foreach ( $ancestors as $ancestor_id ) {
				$items[] = array(
					'label' => get_the_title( $ancestor_id ),
					'url'   => get_permalink( $ancestor_id ),
				);
			}
		}

		return $items;
	}
}

if ( ! function_exists( 'rx_theme_add_post_categories_to_breadcrumbs' ) ) {

	/**
	 * Add post category hierarchy.
	 *
	 * @param array $items   Breadcrumb items.
	 * @param int   $post_id Post ID.
	 * @return array
	 */
	function rx_theme_add_post_categories_to_breadcrumbs( $items, $post_id ) {

		$categories = get_the_category( $post_id );

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

		$primary_category = rx_theme_get_deepest_term( $categories );

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

		$items = rx_theme_add_term_ancestors_to_breadcrumbs( $items, $primary_category );

		$category_link = get_category_link( $primary_category );

		if ( ! is_wp_error( $category_link ) ) {
			$items[] = array(
				'label' => $primary_category->name,
				'url'   => $category_link,
			);
		}

		return $items;
	}
}

if ( ! function_exists( 'rx_theme_add_term_ancestors_to_breadcrumbs' ) ) {

	/**
	 * Add parent terms.
	 *
	 * @param array   $items Breadcrumb items.
	 * @param WP_Term $term  Term object.
	 * @return array
	 */
	function rx_theme_add_term_ancestors_to_breadcrumbs( $items, $term ) {

		if ( ! $term || is_wp_error( $term ) || empty( $term->taxonomy ) ) {
			return $items;
		}

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

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

			foreach ( $ancestors as $ancestor_id ) {
				$ancestor = get_term( $ancestor_id, $term->taxonomy );

				if ( ! $ancestor || is_wp_error( $ancestor ) ) {
					continue;
				}

				$ancestor_link = get_term_link( $ancestor );

				if ( is_wp_error( $ancestor_link ) ) {
					continue;
				}

				$items[] = array(
					'label' => $ancestor->name,
					'url'   => $ancestor_link,
				);
			}
		}

		return $items;
	}
}

if ( ! function_exists( 'rx_theme_get_deepest_term' ) ) {

	/**
	 * Get deepest term from assigned terms.
	 *
	 * @param array $terms Terms.
	 * @return WP_Term|null
	 */
	function rx_theme_get_deepest_term( $terms ) {

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

		$deepest_term  = null;
		$deepest_level = -1;

		foreach ( $terms as $term ) {
			if ( ! $term || is_wp_error( $term ) ) {
				continue;
			}

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

			if ( $level > $deepest_level ) {
				$deepest_level = $level;
				$deepest_term  = $term;
			}
		}

		return $deepest_term;
	}
}

if ( ! function_exists( 'rx_theme_filter_breadcrumb_items' ) ) {

	/**
	 * Clean and filter breadcrumb items.
	 *
	 * @param array $items Breadcrumb items.
	 * @return array
	 */
	function rx_theme_filter_breadcrumb_items( $items ) {

		$clean_items = array();

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

			$clean_items[] = array(
				'label' => wp_strip_all_tags( $item['label'] ),
				'url'   => ! empty( $item['url'] ) ? esc_url_raw( $item['url'] ) : '',
			);
		}

		/**
		 * Remove duplicate consecutive items.
		 */
		$unique_items = array();

		foreach ( $clean_items as $item ) {
			$last_item = end( $unique_items );

			if (
				$last_item &&
				isset( $last_item['label'], $item['label'] ) &&
				$last_item['label'] === $item['label']
			) {
				continue;
			}

			$unique_items[] = $item;
		}

		return apply_filters( 'rx_theme_breadcrumb_items', $unique_items );
	}
}

if ( ! function_exists( 'rx_theme_get_breadcrumb_current_page_number' ) ) {

	/**
	 * Get current pagination number.
	 *
	 * @return int
	 */
	function rx_theme_get_breadcrumb_current_page_number() {

		$paged = 0;

		if ( get_query_var( 'paged' ) ) {
			$paged = absint( get_query_var( 'paged' ) );
		} elseif ( get_query_var( 'page' ) ) {
			$paged = absint( get_query_var( 'page' ) );
		}

		return $paged;
	}
}

if ( ! function_exists( 'rx_theme_breadcrumbs_json_ld' ) ) {

	/**
	 * Output JSON-LD schema.
	 *
	 * @param array $items Breadcrumb items.
	 * @return void
	 */
	function rx_theme_breadcrumbs_json_ld( $items ) {

		if ( empty( $items ) || ! apply_filters( 'rx_theme_breadcrumbs_enable_schema', true ) ) {
			return;
		}

		$schema_items = array();

		foreach ( $items as $index => $item ) {

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

			$schema_items[] = array(
				'@type'    => 'ListItem',
				'position' => $index + 1,
				'name'     => wp_strip_all_tags( $item['label'] ),
				'item'     => esc_url_raw( $url ),
			);
		}

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

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

if ( ! function_exists( 'rx_theme_get_current_url' ) ) {

	/**
	 * Get current URL safely.
	 *
	 * @return string
	 */
	function rx_theme_get_current_url() {

		$scheme = is_ssl() ? 'https://' : 'http://';
		$host   = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : wp_parse_url( home_url(), PHP_URL_HOST );
		$uri    = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/';

		return esc_url_raw( $scheme . $host . $uri );
	}
}

if ( ! function_exists( 'rx_theme_should_display_breadcrumbs' ) ) {

	/**
	 * Decide whether breadcrumbs should display.
	 *
	 * @return bool
	 */
	function rx_theme_should_display_breadcrumbs() {

		$display = true;

		if ( is_front_page() && ! apply_filters( 'rx_theme_breadcrumbs_show_on_front_page', false ) ) {
			$display = false;
		}

		if ( is_admin() || wp_doing_ajax() ) {
			$display = false;
		}

		return apply_filters( 'rx_theme_should_display_breadcrumbs', $display );
	}
}

$rx_breadcrumb_items = rx_theme_get_breadcrumbs_items();
$rx_current_page     = rx_theme_get_breadcrumb_current_page_number();

if ( $rx_current_page > 1 ) {
	$rx_breadcrumb_items[] = array(
		'label' => sprintf(
			/* translators: %d: page number */
			esc_html__( 'Page %d', 'rx-theme' ),
			$rx_current_page
		),
		'url'   => '',
	);
}

$rx_breadcrumb_items = rx_theme_filter_breadcrumb_items( $rx_breadcrumb_items );

if ( rx_theme_should_display_breadcrumbs() && ! empty( $rx_breadcrumb_items ) ) :
	?>

	<nav class="rx-breadcrumbs"
		aria-label="<?php echo esc_attr_x( 'Breadcrumb', 'breadcrumb navigation label', 'rx-theme' ); ?>"
		itemscope
		itemtype="https://schema.org/BreadcrumbList">

		<ol class="rx-breadcrumbs__list">

			<?php foreach ( $rx_breadcrumb_items as $rx_index => $rx_item ) : ?>

				<?php
				$rx_position  = $rx_index + 1;
				$rx_is_last   = count( $rx_breadcrumb_items ) === $rx_position;
				$rx_item_url  = ! empty( $rx_item['url'] ) ? $rx_item['url'] : '';
				$rx_item_name = ! empty( $rx_item['label'] ) ? $rx_item['label'] : '';
				?>

				<li class="rx-breadcrumbs__item<?php echo $rx_is_last ? ' is-current' : ''; ?>"
					itemprop="itemListElement"
					itemscope
					itemtype="https://schema.org/ListItem">

					<?php if ( ! $rx_is_last && ! empty( $rx_item_url ) ) : ?>

						<a class="rx-breadcrumbs__link"
							href="<?php echo esc_url( $rx_item_url ); ?>"
							itemprop="item">

							<span itemprop="name">
								<?php echo esc_html( $rx_item_name ); ?>
							</span>

						</a>

						<span class="rx-breadcrumbs__separator" aria-hidden="true">
							<?php echo esc_html( apply_filters( 'rx_theme_breadcrumb_separator', '›' ) ); ?>
						</span>

					<?php else : ?>

						<span class="rx-breadcrumbs__current"
							itemprop="name"
							aria-current="page">
							<?php echo esc_html( $rx_item_name ); ?>
						</span>

					<?php endif; ?>

					<meta itemprop="position" content="<?php echo esc_attr( $rx_position ); ?>">

				</li>

			<?php endforeach; ?>

		</ol>

	</nav>

	<?php
	rx_theme_breadcrumbs_json_ld( $rx_breadcrumb_items );
endif;

Add this small CSS in style.css

.rx-breadcrumbs {
	font-size: 14px;
	line-height: 1.6;
	margin: 0 0 20px;
	color: #555;
}

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

.rx-breadcrumbs__item {
	display: inline-flex;
	align-items: center;
	gap: 6px;
}

.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;
	color: #111;
}

.rx-breadcrumbs__separator {
	opacity: 0.6;
}

[dir="rtl"] .rx-breadcrumbs__separator {
	transform: rotate(180deg);
}

Use it in your theme files

For example, add this in single.php, page.php, archive.php, index.php, or after your header:

<?php get_template_part( 'template-parts/components/breadcrumbs' ); ?>

Best place:

<?php get_header(); ?>

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

	<?php get_template_part( 'template-parts/components/breadcrumbs' ); ?>

	<!-- Your page content here -->

</main>

<?php get_footer(); ?>

This file is safe for a big RX Theme structure because every helper function is wrapped with function_exists(), so it will not break your site if you later move these functions into inc/helpers/breadcrumbs.php.

Leave a Reply

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