diff --git a/wp-includes/block-template-utils.php b/wp-includes/block-template-utils.php index 66c48db7f1..acbb7724c5 100644 --- a/wp-includes/block-template-utils.php +++ b/wp-includes/block-template-utils.php @@ -1325,20 +1325,46 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' return array( 'front-page', 'home', 'index' ); } - $template_hierarchy = array( $slug ); + $matches = array(); + $template_hierarchy = array( $slug ); // Most default templates don't have `$template_prefix` assigned. - if ( $template_prefix ) { + if ( ! empty( $template_prefix ) ) { list( $type ) = explode( '-', $template_prefix ); - // These checks are needed because the `$slug` above is always added. + // We need these checks because we always add the `$slug` above. if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) { $template_hierarchy[] = $template_prefix; } if ( $slug !== $type ) { $template_hierarchy[] = $type; } - } + } else if ( preg_match( '/^(author|category|archive|tag|page)-.+$/', $slug, $matches ) ) { + $template_hierarchy[] = $matches[1]; + } else if ( preg_match( '/^(taxonomy|single)-(.+)$/', $slug, $matches ) ) { + $type = $matches[1]; + $slug_remaining = $matches[2]; + $items = 'single' === $type ? get_post_types() : get_taxonomies(); + foreach ( $items as $item ) { + if ( ! str_starts_with( $slug_remaining, $item ) ) { + continue; + } + + // If $slug_remaining is equal to $post_type or $taxonomy we have + // the single-$post_type template or the taxonomy-$taxonomy template. + if ( $slug_remaining === $item ) { + $template_hierarchy[] = $type; + break; + } + + // If $slug_remaining is single-$post_type-$slug template. + if ( strlen( $slug_remaining ) > strlen( $item ) + 1 ) { + $template_hierarchy[] = "$type-$item"; + $template_hierarchy[] = $type; + break; + } + } + } // Handle `archive` template. if ( str_starts_with( $slug, 'author' ) || @@ -1353,7 +1379,6 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' if ( 'attachment' === $slug ) { $template_hierarchy[] = 'single'; } - // Handle `singular` template. if ( str_starts_with( $slug, 'single' ) || @@ -1362,8 +1387,6 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' ) { $template_hierarchy[] = 'singular'; } - $template_hierarchy[] = 'index'; - return $template_hierarchy; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 6076283458..6bd965e428 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.2-alpha-55193'; +$wp_version = '6.2-alpha-55194'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.