Using is_page_template to check if current page is using a specific template in WordPress

The WordPress is_page_template function is used to check if a specific page template is being used for the current page. It returns a boolean value, true if the page is using the specified template and false if it is not.

This function can be useful for conditional logic within a WordPress theme or plugin. For example, it can be used to display different content or styles based on the page template being used. This allows for more dynamic and customized website layouts and functionality.

Parameters Accepted by the WordPress is_page_template Function

The is_page_template function accepts the following parameter:

  • $template (string|string[], optional, default value: ”): The specific template filename or array of templates to match.

Value Returned by the WordPress is_page_template Function

The is_page_template function returns:

  • bool: True on success, false on failure.

Examples

How to check if the current page is using a specific template

if ( is_page_template( 'template-custom.php' ) ) {
 // Do something specific for pages using the 'template-custom.php' template
} else {
 // Do something else for pages not using the 'template-custom.php' template
}

This code snippet uses the is_page_template function to check if the current page is using a specific template called 'template-custom.php'. If the page is using the specified template, it executes a specific block of code. Otherwise, it executes a different block of code.

How to display different content based on page template

if ( is_page_template( 'template-about.php' ) ) {
 // Display content specific to the 'template-about.php' template
 get_template_part( 'content', 'about' );
} elseif ( is_page_template( 'template-services.php' ) ) {
 // Display content specific to the 'template-services.php' template
 get_template_part( 'content', 'services' );
} else {
 // Display default content for other pages
 get_template_part( 'content', 'default' );
}

In this code snippet, we use the is_page_template function to check if the current page is using a specific template. Depending on the template being used, it includes different content using the get_template_part function.

How to redirect to a specific page if using a certain template

if ( is_page_template( 'template-landing.php' ) ) {
 // Redirect to a specific page if using the 'template-landing.php' template
 wp_redirect( home_url( '/landing-page' ) );
 exit;
}

This code snippet demonstrates how to use the is_page_template function to check if the current page is using a specific template. If the page is using the specified template, it redirects the user to a different page using the wp_redirect function.

Conclusion

The is_page_template function is a valuable tool for developers working with WordPress. It provides a simple and efficient way to check if a specific page is using a particular template, allowing for conditional logic and customization based on page templates. By understanding how to properly use this function, developers can enhance the flexibility and functionality of their WordPress websites.

With the ability to easily determine the template being used on a specific page, developers can create more dynamic and tailored user experiences. Whether it’s adjusting layout, adding specific functionality, or applying custom styling, the is_page_template function empowers developers to make targeted changes based on the template being utilized.

By leveraging the capabilities of the is_page_template function, developers can streamline their code, improve site performance, and deliver a more personalized experience for site visitors. This function is a valuable addition to the toolkit of any WordPress developer, providing a straightforward means to enhance the customization options available within the platform.

Related WordPress Functions