The get_page_template_slug() WordPress PHP function retrieves the specific template filename for a given post.
Usage
get_page_template_slug( $post )
Input: $post = 12
Output: 'template-custom.php'
Parameters
$post
(int|WP_Post) – Optional. Post ID or WP_Post object. Default is global$post
. Default: null
More information
See WordPress Developer Resources: get_page_template_slug()
Examples
Display the page template filename of the current page
To display the page template filename of the current page, use the following code:
echo esc_html( get_page_template_slug( $post->ID ) );
Get all pages using a specific template
To find all the pages that use a particular page template filename, use the following code:
function wpdocs_get_pages_by_template_filename( $page_template_filename ) { return get_pages( array( 'meta_key' => '_wp_page_template', 'meta_value' => $page_template_filename )); } $pages = wpdocs_get_pages_by_template_filename( 'templates/offers.php' );
This will return an array or false for a list of pages matching the specified page template filename.
Display titles of all pages using a specific template
To display the titles of all pages using a specific template, use the following code:
$pages = wpdocs_get_pages_by_template_filename( 'templates/about.php' ); foreach ( $pages as $page ) { echo esc_html( $page->post_title ); }
Retrieve page template slug for a specific post ID
To retrieve the page template slug for a specific post ID, use the following code:
$template_slug = get_page_template_slug( 42 ); echo esc_html( $template_slug );
Check if a page is using a specific template
To check if a page is using a specific template, use the following code:
$template_slug = get_page_template_slug( $post->ID ); if ( 'template-custom.php' === $template_slug ) { echo 'This page is using the custom template.'; } else { echo 'This page is not using the custom template.'; }