get_page_template

The timeline below displays how wordpress function get_page_template has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Retrieves path of page template in current or parent template.
 *
 * Note: For block themes, use locate_block_template() function instead.
 *
 * 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 {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
 *
 * @since 1.5.0
 * @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.
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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);
}

WordPress Version: 6.2

/**
 * Retrieves path of page template in current or parent template.
 *
 * Note: For block themes, use locate_block_template() function instead.
 *
 * 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 {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
 *
 * @since 1.5.0
 * @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.
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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);
}

WordPress Version: 6.1

/**
 * Retrieves path of page template in current or parent template.
 *
 * Note: For block themes, use locate_block_template function instead.
 *
 * 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 {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
 *
 * @since 1.5.0
 * @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.
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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);
}

WordPress Version: 5.4

/**
 * Retrieve path of page template in current or parent template.
 *
 * 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 {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
 *
 * @since 1.5.0
 * @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.
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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);
}

WordPress Version: 4.9

/**
 * Retrieve path of page template in current or parent template.
 *
 * 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 {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
 *
 * @since 1.5.0
 * @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.
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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);
}

WordPress Version: 4.7

/**
 * Retrieve path of page template in current or parent template.
 *
 * 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 is filterable via the {@see 'page_template_hierarchy'} hook.
 * The template path is filterable via the {@see 'page_template'} hook.
 *
 * @since 1.5.0
 * @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.
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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);
}

WordPress Version: 4.3

/**
 * Retrieve path of page template in current or parent template.
 *
 * Will first look for the specifically assigned page template.
 * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
 * and finally 'page.php'.
 *
 * The template path is filterable via the dynamic {@see '$type_template'} hook,
 * e.g. 'page_template'.
 *
 * @since 1.5.0
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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) {
        $templates[] = "page-{$pagename}.php";
    }
    if ($id) {
        $templates[] = "page-{$id}.php";
    }
    $templates[] = 'page.php';
    return get_query_template('page', $templates);
}

WordPress Version: 3.9

/**
 * Retrieve path of page template in current or parent template.
 *
 * Will first look for the specifically assigned page template.
 * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
 * and finally 'page.php'.
 *
 * The template path is filterable via the 'page_template' hook.
 *
 * @since 1.5.0
 *
 * @see get_query_template()
 *
 * @return string Full path to page template file.
 */
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) {
        $templates[] = "page-{$pagename}.php";
    }
    if ($id) {
        $templates[] = "page-{$id}.php";
    }
    $templates[] = 'page.php';
    return get_query_template('page', $templates);
}

WordPress Version: 3.7

/**
 * Retrieve path of page template in current or parent template.
 *
 * Will first look for the specifically assigned page template.
 * Then will search for 'page-{slug}.php', followed by 'page-{id}.php',
 * and finally 'page.php'.
 *
 * @since 1.5.0
 *
 * @return string
 */
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) {
        $templates[] = "page-{$pagename}.php";
    }
    if ($id) {
        $templates[] = "page-{$id}.php";
    }
    $templates[] = 'page.php';
    return get_query_template('page', $templates);
}