Retrieves path of page template in current or parent template.
Description
Note: For block themes, use locate_block_template() function instead.
The hierarchy for this template looks like:
- {Page Template}.php
- page-{page_name}.php
- page-{id}.php
- page.php
An example of this is:
- page-templates/full-width.php
- page-about.php
- page-4.php
- page.php
The template hierarchy and template path are filterable via the ‘$type_template_hierarchy’ and ‘$type_template’ dynamic hooks, where $type
is ‘page’.
See also
Source
function get_page_template() {
$id = get_queried_object_id();
$template = get_page_template_slug();
$pagename = get_query_var( 'pagename' );
if ( ! $pagename && $id ) {
/*
* If a static page is set as the front page, $pagename will not be set.
* Retrieve it from the queried object.
*/
$post = get_queried_object();
if ( $post ) {
$pagename = $post->post_name;
}
}
$templates = array();
if ( $template && 0 === validate_file( $template ) ) {
$templates[] = $template;
}
if ( $pagename ) {
$pagename_decoded = urldecode( $pagename );
if ( $pagename_decoded !== $pagename ) {
$templates[] = "page-{$pagename_decoded}.php";
}
$templates[] = "page-{$pagename}.php";
}
if ( $id ) {
$templates[] = "page-{$id}.php";
}
$templates[] = 'page.php';
return get_query_template( 'page', $templates );
}
Display the filename of the page template used to render a page (printed within an HTML comment, in this example) :
The link for $type_template is not found on this page. It needs updating.
this function is probably useless / not working as intended with Gutenberg Full Site Edit, as it returns /wp-includes/template-canvas.php