get_page_template()WP 1.5.0

Gets the path to the page template in the current or parent template.

The hierarchy for this template looks as follows:

  1. {page template}.php
  2. page-{page_name}.php
  3. page-{id}.php
  4. page.php

In reality, it looks something like this:

  1. page-templates/full-width.php
  2. page-about.php
  3. page-4.php
  4. page.php

The template hierarchy and the path to the template can be changed using dynamic filters (type)_template_hierarchy and (type)_template, where $type is page.

Since version 4.7.0 and above, the template hierarchy has added the file page-{$pagename_decoded}.php, where $pagename_decoded = urldecode( page_name ).

1 time — 0.0007071 sec (slow) | 50000 times — 1.84 sec (fast)

No Hooks.

Returns

String. Full path to the page template file.

Usage

get_page_template();

Examples

0

#1 Get the template file of the current page

echo get_page_template();
//> /home/public_html/wp-content/themes/theme-name/page.php

Notes

Changelog

Since 1.5.0 Introduced.
Since 4.7.0 The decoded form of page-{page_name}.php was added to the top of the template hierarchy when the page name contains multibyte characters.

get_page_template() code WP 6.8.3

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 );
}