WordPress at a glance

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

    subfolder/file-name.php
  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 );
  • Since 4.7.0 Now works with any post type, not just pages.
Works based on: get_post(), get_post_meta()

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

Code of get_page_template_slug: wp-includes/post-template.php VER 5.0.1

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

Related Functions

From category: Pages

No comments
    Hello, !     Log In . Register