函数文档

get_page_template_slug()

💡 云策文档标注

概述

get_page_template_slug() 函数用于获取指定文章的自定义页面模板文件名。它通过查询 '_wp_page_template' 自定义字段来工作,适用于所有文章类型,而不仅仅是页面。

关键要点

  • 参数 $post 可选,可以是文章 ID 或 WP_Post 对象,默认为全局 $post。
  • 返回值:成功时返回模板文件名(字符串),使用默认模板时返回空字符串,文章不存在时返回 false。
  • 模板文件名存储在 wp_postmeta 表的 '_wp_page_template' 自定义字段中,子目录路径会包含在值中。
  • 从 WordPress 4.7.0 开始,此函数支持所有文章类型,不再仅限于页面。

代码示例

// 获取当前页面的模板文件名
echo get_page_template_slug( get_the_ID() );

// 自定义函数:根据模板文件名查找页面
function wpdocs_get_pages_by_template_filename( $page_template_filename ) {
    return get_pages( array(
        'meta_key' => '_wp_page_template',
        'meta_value' => $page_template_filename
    ) );
}
$pages = wpdocs_get_pages_by_template_filename( 'templates/offers.php' );

注意事项

  • 如果 '_wp_page_template' 字段为空或值为 'default',函数返回空字符串。
  • 以下划线开头的自定义字段(如 '_wp_page_template')不会在编辑屏幕的自定义字段模块中显示。
  • 也可以使用 get_post_meta( $post->ID, '_wp_page_template', true ) 直接获取模板元数据。

📄 原文内容

Gets the specific template filename for a given post.

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.

Default:null

Return

string|false Page template filename. Returns an empty string when the default page template is in use. Returns false if the post does not exist.

More Information

The filename of a Page’s assigned custom template is stored as the value of a Custom Field with a key named '_wp_page_template' (in the wp_postmeta database table). If the template is stored in a Theme’s subdirectory (or a Parent Theme’s subdirectory of a Child Theme), the value of the wp_postmeta is both the folder and file names, e.g.

my-templates/my-custom-template.php

The function get_page_template_slug() returns an empty string when the value of '_wp_page_template' is either empty or 'default'.

Custom fields starting with an underscore do not display in the Edit screen’s Custom Fields module. To retrieve a Page’s custom template metadata, you can also use:

get_post_meta( $post->ID, '_wp_page_template', true )

Source

function get_page_template_slug( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$template = get_post_meta( $post->ID, '_wp_page_template', true );

	if ( ! $template || 'default' === $template ) {
		return '';
	}

	return $template;
}

Changelog

Version Description
4.7.0 Now works with any post type, not just pages.
3.4.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    If you need reverse engineering to find all the pages that are working under a particular page template filename, this is one solution that may work for you.

    function wpdocs_get_pages_by_template_filename( $page_template_filename ) {
    	return get_pages( array(
    		'meta_key' => '_wp_page_template',
    		'meta_value' => $page_template_filename
    	) );
    }

    You can use this function for example:

    $pages = wpdocs_get_pages_by_template_filename( 'templates/offers.php' );

    And it will return (array|false) list of pages matching by that page template filename.

    Generally, it happens that inside a theme you build, you need to find a certain page that works under a special custom template and that you need to dynamically access its ID, content, title, etc, and this function will help for that.