get_page_template_slug() WordPress Function

The get_page_template_slug() function allows you to get the page template slug for a given page. This is useful for determining which template is being used on a page, and can be used in conjunction with the get_page_templates() function.

get_page_template_slug( int|WP_Post $post = null ) #

Gets the specific template filename for a given post.


Parameters

$post

(int|WP_Post)(Optional) Post ID or WP_Post object. Default is global $post.

Default value: null


Top ↑

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.


Top ↑

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 )

 


Top ↑

Source

File: wp-includes/post-template.php

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


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.