resolve_block_template()
最后更新于:2021-11-27 23:30:07
resolve_block_template( string$template_type, string[]$template_hierarchy)响应 the correct ‘gc_template’ to render for the request template type.
参数
- $template_type
-
(string) (Required) The current template type.
- $template_hierarchy
-
(string[]) (Required) The current template hierarchy, ordered by priority.
响应
(GC_Block_Template|null) template A template object, or null if none could be found.
源文件
文件: gc-includes/block-template.php
function resolve_block_template( $template_type, $template_hierarchy ) {
if ( ! $template_type ) {
return null;
}
if ( empty( $template_hierarchy ) ) {
$template_hierarchy = array( $template_type );
}
$slugs = array_map(
'_strip_template_file_suffix',
$template_hierarchy
);
// Find all potential templates 'gc_template' post matching the hierarchy.
$query = array(
'theme' => gc_get_theme()->get_stylesheet(),
'slug__in' => $slugs,
);
$templates = get_block_templates( $query );
// Order these templates per slug priority.
// Build map of template slugs to their priority in the current hierarchy.
$slug_priorities = array_flip( $slugs );
usort(
$templates,
function ( $template_a, $template_b ) use ( $slug_priorities ) {
return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
}
);
return count( $templates ) ? $templates[0] : null;
}