is_page_template()WP 2.5.0

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.

1 time — 0.00016 sec (fast) | 50000 times — 2.95 sec (fast)

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

0

#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
}
0

#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
}
0

#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
}
0

#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.

Code of is_page_template() WP 6.0

function is_page_template( $template = '' ) {
	if ( ! is_singular() ) {
		return false;
	}

	$page_template = get_page_template_slug( get_queried_object_id() );

	if ( empty( $template ) ) {
		return (bool) $page_template;
	}

	if ( $template == $page_template ) {
		return true;
	}

	if ( is_array( $template ) ) {
		if ( ( in_array( 'default', $template, true ) && ! $page_template )
			|| in_array( $page_template, $template, true )
		) {
			return true;
		}
	}

	return ( 'default' === $template && ! $page_template );
}