render_block_core_breadcrumbs()WP 7.0.0

Renders the core/breadcrumbs block on the server.

Hooks from the function

Returns

String. Returns the post breadcrumb for hierarchical post types.

Usage

render_block_core_breadcrumbs( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
$content(string) (required)
Block default content.
$block(WP_Block) (required)
Block instance.

Changelog

Since 7.0.0 Introduced.

render_block_core_breadcrumbs() code WP 7.0

function render_block_core_breadcrumbs( $attributes, $content, $block ) {
	$is_front_page = is_front_page();

	if ( ! $attributes['showOnHomePage'] && $is_front_page ) {
		return '';
	}

	$is_home          = is_home();
	$page_for_posts   = get_option( 'page_for_posts' );
	$breadcrumb_items = array();

	if ( $attributes['showHomeItem'] ) {
		// We make `home` a link if not on front page, or if front page
		// is set to a custom page and is paged.
		if ( ! $is_front_page || ( 'page' === get_option( 'show_on_front' ) && (int) get_query_var( 'page' ) > 1 ) ) {
			$breadcrumb_items[] = array(
				'label' => __( 'Home' ),
				'url'   => home_url( '/' ),
			);
		} else {
			$breadcrumb_items[] = block_core_breadcrumbs_create_item( __( 'Home' ), block_core_breadcrumbs_is_paged() );
		}
	}

	// Handle home.
	if ( $is_home ) {
		// These checks are explicitly nested in order not to execute the `else` branch.
		if ( $page_for_posts ) {
			$breadcrumb_items[] = block_core_breadcrumbs_create_item( block_core_breadcrumbs_get_post_title( $page_for_posts ), block_core_breadcrumbs_is_paged() );
		}
		if ( block_core_breadcrumbs_is_paged() ) {
			$breadcrumb_items[] = block_core_breadcrumbs_create_page_number_item();
		}
	} elseif ( $is_front_page ) {
		// Handle front page.
		// This check is explicitly nested in order not to execute the `else` branch.
		// If front page is set to custom page and is paged, add the page number.
		if ( (int) get_query_var( 'page' ) > 1 ) {
			$breadcrumb_items[] = block_core_breadcrumbs_create_page_number_item( 'page' );
		}
	} elseif ( is_search() ) {
		// Handle search results.
		$is_paged = block_core_breadcrumbs_is_paged();
		/* translators: %s: search query */
		$text               = sprintf( __( 'Search results for: "%s"' ), wp_trim_words( get_search_query(), 10 ) );
		$breadcrumb_items[] = block_core_breadcrumbs_create_item( $text, $is_paged );
		// Add the "Page X" as the current page if paginated.
		if ( $is_paged ) {
			$breadcrumb_items[] = block_core_breadcrumbs_create_page_number_item();
		}
	} elseif ( is_404() ) {
		// Handle 404 pages.
		$breadcrumb_items[] = array(
			'label' => __( 'Page not found' ),
		);
	} elseif ( is_archive() ) {
		// Handle archive pages (taxonomy, post type, date, author archives).
		$archive_breadcrumbs = block_core_breadcrumbs_get_archive_breadcrumbs();
		if ( ! empty( $archive_breadcrumbs ) ) {
			$breadcrumb_items = array_merge( $breadcrumb_items, $archive_breadcrumbs );
		}
	} else {
		// Handle single post/page breadcrumbs.
		if ( ! isset( $block->context['postId'] ) || ! isset( $block->context['postType'] ) ) {
			return '';
		}

		$post_id   = $block->context['postId'];
		$post_type = $block->context['postType'];

		$post = get_post( $post_id );
		if ( ! $post ) {
			return '';
		}

		// For non-hierarchical post types with parents (e.g., attachments), build trail for the parent.
		$post_parent = $post->post_parent;
		$parent_post = null;
		if ( ! is_post_type_hierarchical( $post_type ) && $post_parent ) {
			$parent_post = get_post( $post_parent );
			if ( $parent_post ) {
				$post_id     = $parent_post->ID;
				$post_type   = $parent_post->post_type;
				$post_parent = $parent_post->post_parent;
			}
		}

		// Determine breadcrumb type.
		// Some non-hierarchical post types (e.g., attachments) can have parents.
		// Use hierarchical breadcrumbs if a parent exists, otherwise use taxonomy breadcrumbs.
		$show_terms = false;
		if ( ! is_post_type_hierarchical( $post_type ) && ! $post_parent ) {
			$show_terms = true;
		} elseif ( empty( get_object_taxonomies( $post_type, 'objects' ) ) ) {
			$show_terms = false;
		} else {
			$show_terms = $attributes['prefersTaxonomy'];
		}

		// Add post type archive link if applicable.
		$post_type_object = get_post_type_object( $post_type );
		$archive_link     = get_post_type_archive_link( $post_type );
		if ( $archive_link && untrailingslashit( home_url() ) !== untrailingslashit( $archive_link ) ) {
			$label = $post_type_object->labels->archives;
			if ( 'post' === $post_type && $page_for_posts ) {
				$label = block_core_breadcrumbs_get_post_title( $page_for_posts );
			}
			$breadcrumb_items[] = array(
				'label' => $label,
				'url'   => $archive_link,
			);
		}
		// Build breadcrumb trail based on hierarchical structure or taxonomy terms.
		if ( ! $show_terms ) {
			$breadcrumb_items = array_merge( $breadcrumb_items, block_core_breadcrumbs_get_hierarchical_post_type_breadcrumbs( $post_id ) );
		} else {
			$breadcrumb_items = array_merge( $breadcrumb_items, block_core_breadcrumbs_get_terms_breadcrumbs( $post_id, $post_type ) );
		}

		// Add post title: linked when viewing a paginated page, plain text otherwise.
		$is_paged = (int) get_query_var( 'page' ) > 1 || (int) get_query_var( 'cpage' ) > 1;
		$title    = block_core_breadcrumbs_get_post_title( $post );

		if ( $is_paged ) {
			$breadcrumb_items[] = array(
				'label'      => $title,
				'url'        => get_permalink( $post ),
				'allow_html' => true,
			);
			$breadcrumb_items[] = block_core_breadcrumbs_create_page_number_item( (int) get_query_var( 'cpage' ) > 1 ? 'cpage' : 'page' );
		} else {
			$breadcrumb_items[] = array(
				'label'      => $title,
				'allow_html' => true,
			);
		}
	}

	// Remove current item if disabled.
	if ( ! $attributes['showCurrentItem'] && ! empty( $breadcrumb_items ) ) {
		array_pop( $breadcrumb_items );
	}

	/**
	 * Filters the breadcrumb items array before rendering.
	 *
	 * Allows developers to modify, add, or remove breadcrumb items.
	 *
	 * @since 7.0.0
	 *
	 * @param array[] $breadcrumb_items {
	 *     Array of breadcrumb item data.
	 *
	 *     @type string $label      The breadcrumb text.
	 *     @type string $url        Optional. The breadcrumb link URL.
	 *     @type bool   $allow_html Optional. Whether to allow HTML in the label.
	 *                              When true, the label will be sanitized with wp_kses_post(),
	 *                              allowing only safe HTML tags. When false or omitted, all HTML
	 *                              will be escaped with esc_html(). Default false.
	 * }
	 */
	$breadcrumb_items = apply_filters( 'block_core_breadcrumbs_items', $breadcrumb_items );

	if ( empty( $breadcrumb_items ) ) {
		return '';
	}

	$wrapper_attributes = get_block_wrapper_attributes(
		array(
			'style'      => '--separator: "' . addcslashes( $attributes['separator'], '\\"' ) . '";',
			'aria-label' => __( 'Breadcrumbs' ),
		)
	);

	$breadcrumb_html = sprintf(
		'<nav %s><ol>%s</ol></nav>',
		$wrapper_attributes,
		implode(
			'',
			array_map(
				static function ( $item ) {
					$label = ! empty( $item['allow_html'] ) ? wp_kses_post( $item['label'] ) : esc_html( $item['label'] );
					if ( ! empty( $item['url'] ) ) {
						return '<li><a href="' . esc_url( $item['url'] ) . '">' . $label . '</a></li>';
					}
					return '<li><span aria-current="page">' . $label . '</span></li>';
				},
				$breadcrumb_items
			)
		)
	);

	return $breadcrumb_html;
}