block_core_navigation_get_fallback_blocks()WP 1.0

Retrieves the appropriate fallback to be used on the front of the site when there is no menu assigned to the Nav block.

This aims to mirror how the fallback mechanic for wp_nav_menu works. See https://developer.wordpress.org/reference/functions/wp_nav_menu/#more-information.

Hooks from the function

Return

Array. the array of blocks to be used as a fallback.

Usage

block_core_navigation_get_fallback_blocks();

block_core_navigation_get_fallback_blocks() code WP 6.1.1

function block_core_navigation_get_fallback_blocks() {
	$page_list_fallback = array(
		array(
			'blockName' => 'core/page-list',
		),
	);

	$registry = WP_Block_Type_Registry::get_instance();

	// If `core/page-list` is not registered then return empty blocks.
	$fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array();

	// Default to a list of Pages.

	$navigation_post = block_core_navigation_get_most_recently_published_navigation();

	// Prefer using the first non-empty Navigation as fallback if available.
	if ( $navigation_post ) {
		$maybe_fallback = block_core_navigation_filter_out_empty_blocks( parse_blocks( $navigation_post->post_content ) );

		// Normalizing blocks may result in an empty array of blocks if they were all `null` blocks.
		// In this case default to the (Page List) fallback.
		$fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks;
	}

	/**
	 * Filters the fallback experience for the Navigation block.
	 *
	 * Returning a falsey value will opt out of the fallback and cause the block not to render.
	 * To customise the blocks provided return an array of blocks - these should be valid
	 * children of the `core/navigation` block.
	 *
	 * @since 5.9.0
	 *
	 * @param array[] default fallback blocks provided by the default block mechanic.
	 */
	return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks );
}