Find out whether a template file is used by a page and get that page -Wordpress

11,164

The function get_page_template_slug( $post_id ) will return the slug of the currently assigned page template (or an empty string if no template has been assigned - or false if the $post_id does not correspond to an actual page). You can easily use this anywhere (in The Loop, or outside) to determine whether any page has been assigned a page template.

is_page_template(); function will return true when template is used. you can also pass file name to check if particular template is applied or not.

if ( is_page_template('about.php') ) {
// Returns true when 'about.php' is being used.
} else {
// Returns false when 'about.php' is not being used.
}

There is one more method too. You can use get_post_meta to get value of applied template.

global $post;
get_post_meta($post->ID,'_wp_page_template',true);
Share:
11,164
DebbieMiller
Author by

DebbieMiller

Updated on August 17, 2022

Comments

  • DebbieMiller
    DebbieMiller over 1 year

    I'm developing now a wordpress theme, and wondering if there is an option to know if a template file is being used by a page? I need the link to that page... thanks!