is_page_template()
Checks if the template file is used to display the current page. You can specify the name of the file.
You can specify the name of a specific file to check. For example about.php. If this file is used to generate the page, the function will return true.
Does not work inside the WordPress loop.
If the name of the template file is specified and the file is located in a nested folder, then the name of the nested folder must also be specified:
is_page_template( 'templates/about.php' ) // sometimes (depends on how you specify templates) is_page_template( '/templates/about.php' )
No Hooks.
Returns
true|false.
Usage
if( is_page_template( $template ) ){
// ...
}
- $template(string/array)
- The name of the template file (with extension). Since version 4.2, you can specify multiple names in an array.
Default: ''
Examples
#1 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
} #2 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
} #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. |