The get_block_template() WordPress PHP function retrieves a single unified template object using its id.
Usage
$template_object = get_block_template( 'theme_slug/template_slug', 'wp_template' );
Parameters
- $id (string) – Required. Template unique identifier (example: theme_slug/template_slug).
- $template_type (string) – Optional. Template type: ‘wp_template’ or ‘wp_template_part’. Default is ‘wp_template’.
More information
See WordPress Developer Resources: get_block_template
Examples
Retrieve a template object
Retrieve a template object by providing the template id.
$template_object = get_block_template( 'mytheme/header' );
Retrieve a template part object
Retrieve a template part object by providing the template id and specifying the ‘wp_template_part’ type.
$template_part_object = get_block_template( 'mytheme/navigation', 'wp_template_part' );
Display template content
Retrieve a template object and display its content.
$template_object = get_block_template( 'mytheme/page' ); if ( $template_object ) { echo $template_object->content; }
Check if a template exists
Check if a specific template exists by trying to retrieve it and testing the result.
$template_object = get_block_template( 'mytheme/single-post' ); if ( $template_object ) { echo 'The template exists!'; } else { echo 'The template does not exist.'; }
Get template object properties
Retrieve a template object and access its properties.
$template_object = get_block_template( 'mytheme/footer' ); if ( $template_object ) { echo 'Template ID: ' . $template_object->id . '<br>'; echo 'Template Slug: ' . $template_object->slug . '<br>'; echo 'Template Title: ' . $template_object->title . '<br>'; }