get_block_template( string $id, string $template_type = 'wp_template' )
Retrieves a single unified template object using its id.
Parameters
- $id
-
(string) (Required) Template unique identifier (example: theme_slug//template_slug).
- $template_type
-
(string) (Optional) wp_template.
Default value: 'wp_template'
Return
(WP_Block_Template|null) Template.
Source
File: wp-includes/block-template-utils.php
function get_block_template( $id, $template_type = 'wp_template' ) { $parts = explode( '//', $id, 2 ); if ( count( $parts ) < 2 ) { return null; } list( $theme, $slug ) = $parts; $wp_query_args = array( 'post_name__in' => array( $slug ), 'post_type' => $template_type, 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), 'posts_per_page' => 1, 'no_found_rows' => true, 'tax_query' => array( array( 'taxonomy' => 'wp_theme', 'field' => 'name', 'terms' => $theme, ), ), ); $template_query = new WP_Query( $wp_query_args ); $posts = $template_query->posts; if ( count( $posts ) > 0 ) { $template = _build_template_result_from_post( $posts[0] ); if ( ! is_wp_error( $template ) ) { return $template; } } return null; }
Changelog
Version | Description |
---|---|
5.8.0 | Introduced. |
© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_block_template