On this page
get_page_template_slug( int|WP_Post $post = null ): string|false
Gets the specific template filename for a given post.
Parameters
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
File: wp-includes/post-template.php. View all references
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;
}
Related
Uses
| Uses | Description |
|---|---|
| get_post_meta() wp-includes/post.php | Retrieves a post meta field for the given post ID. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Posts_Controller::check_template() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Checks whether the template is valid for the given post. |
| WP_REST_Posts_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares a single post output for response. |
| get_single_template() wp-includes/template.php | Retrieves path of single template in current or parent template. Applies to single Posts, single Attachments, and single custom post types. |
| get_page_template() wp-includes/template.php | Retrieves path of page template in current or parent template. |
| is_page_template() wp-includes/post-template.php | Determines whether the current post uses a page template. |
| get_body_class() wp-includes/post-template.php | Retrieves an array of the class names for the body element. |
| wp_xmlrpc_server::_prepare_page() wp-includes/class-wp-xmlrpc-server.php | Prepares page data for return in an XML-RPC object. |
| _WP_Editors::editor_settings() wp-includes/class-wp-editor.php |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_page_template_slug