is_page_template()
Whether currently in a page template.
This template tag allows you to determine if you are in a page template. You can optionally provide a template filename or array of template filenames and then the check will be specific to that template.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
No Hooks.
Return
true|false
. True on success, false on failure.
Usage
is_page_template( $template );
- $template(string|string[])
- The specific template filename or array of templates to match.
Default: ''
Examples
#1 Check if the page has a separate template
This example shows how to check if the current static page has a separate template file to display its content:
if ( is_page_template() ) { // a separate template is used } else { // normal output }
#2 Check whether the file about.php is used to display the page
If the template page "about" is displayed, we can determine it by the file that is responsible for displaying the content of this page.
Suppose we have a separate template file about.php for this page. Then the check code will look like this:
if ( is_page_template('about.php') ) { // this will work if the file 'about.php' is used } else { // this will work if 'about.php' is not used }
#3 A number of template names to check out
You can also specify multiple templates by passing an array of template names.
if( is_page_template( [ 'about.php', 'sitemap.php' ] ) ) { // one of the templates is used } else { // normal output, no templates }
#4 Template from sub-directory
If your page template resides within a directory, you can use something like this:
if ( is_page_template( 'directory-name/page-about.php' ) ) { // about.php is used } else { // about.php is not used }
This can be useful if you’re using multiple page templates and want to keep your files organised.
Changelog
Since 2.5.0 | Introduced. |
Since 4.2.0 | The $template parameter was changed to also accept an array of page templates. |
Since 4.7.0 | Now works with any post type, not just pages. |