WordPress at Your Fingertips

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.

  1. 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:

  2. The function will return an empty string if there is no meta field "_wp_page_template" or it's the value is "default".

  3. 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.


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.


get_page_template_slug( $post );
Post ID or WP_Post object.
Default: global $post


#1 Print the template name of page 123

echo get_page_template_slug( 123 ); //> file-name.php


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

Code of get_page_template_slug() WP 5.8

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;

From category: Pages

No comments
    Log In