get_page_template() WordPress Function

The get_page_template() function is used to retrieve the path of a page template. This function can be used to check if a page template exists for a specific page.

get_page_template() #

Retrieve path of page template in current or parent template.


Description

The hierarchy for this template looks like:

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

An example of this is:

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

The template hierarchy and template path are filterable via the ‘$type_template_hierarchy’ and ‘$type_template’ dynamic hooks, where $type is ‘page’.

Top ↑

See also


Top ↑

Return

(string) Full path to page template file.


Top ↑

Source

File: wp-includes/template.php

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


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.