get_page_template()WP 1.5.0

Получает путь к шаблону страницы в текущем или родительском шаблоне.

Иерархия для этого шаблона выглядит следующим образом:

  1. {шаблон страницы}.php
  2. page-{page_name}.php
  3. page-{id}.php
  4. page.php

В реальности это выглядит примерно так:

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

Узнайте больше о иерархии темы WordPress.

Иерархию шаблонов и путь к шаблону можно изменить с помощью динамических фильтров (type)_template_hierarchy и (type)_template, где $type - это page.

C версии 4.7.0 вверх иерархии шаблонов добавлен файл page-{$pagename_decoded}.php, где $pagename_decoded = urldecode( page_name ).

Работает на основе: get_query_template()
1 раз — 0.0007071 сек (медленно) | 50000 раз — 1.84 сек (быстро)

Хуков нет.

Возвращает

Строку. Полный путь до файла шаблона страницы.

Использование

get_page_template();

Примеры

0

#1 Получим файл шаблона текущей страницы

echo get_page_template();

// получим: /home/public_html/wp-content/themes/theme-name/page.php

Заметки

Список изменений

С версии 1.5.0 Введена.
С версии 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() WP 6.5.2

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