get_page_template_slug() WP 3.4.0
Gets the specific template name for a given post.
Gets the name of the php file, which is set as a template for the specified post. If the template for the post is not set empty string will be returned.
Notes
-
The template file name is saves to meta-field _wp_page_template (wp_postmeta database table). If the real file is located in a subdirectory of the parent or child theme, the value of wp_postmeta will be like this:
subfolder/file-name.php
-
The function will return an empty string if there is no meta field "_wp_page_template" or it's the value is "default".
-
Custom fields starting with
_
are not displayed in the "custom fields" metabox of edit-post page. Such names are considered as system in WordPress. Therefore, to get the name, you can use:$template = get_post_meta( $post->ID, '_wp_page_template', true );
No Hooks.
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.
Usage
get_page_template_slug( $post );
- $post(int|WP_Post)
- Post ID or WP_Post object.
Default: global $post
Examples
#1 Print the template name of page 123
echo get_page_template_slug( 123 ); //> file-name.php
Changelog
Since 3.4.0 | Introduced. |
Since 4.7.0 | Now works with any post type, not just pages. |
Code of get_page_template_slug() get page template slug WP 5.6.2
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;
}