How to detect the default page template in WordPress
This is a quick post. When you need to check which template a page is using, you’d normally call is_page_template(). But how do you detect that a page uses the default template, the one without a name in the Page Attributes box? There is no template filename to pass.
Problem: How do you check whether the default page template is in use?
Solution: Here are two ways to handle this.
Option 1: a page with no custom template assigned
if ( is_page() && ! is_page_template() ) {
// The default template is active.
}
Called without arguments, is_page_template() returns true when the page has any custom template assigned. So this condition means: it is a page, and no custom template is selected for it. This is the more reliable check and the one to use in most cases.
Option 2: check the template file
if ( is_page() && 'page.php' === basename( get_page_template() ) ) {
// page.php is being used.
}
This checks which file WordPress actually loads. It answers a slightly different question: if the theme has no page.php, a page with the default template is rendered by singular.php or index.php, and this condition is false. Use it only when you need to know that exactly page.php is in use.
Outside the main query
Both checks above work for the page being viewed. For any other page, read the assigned template by ID. An empty string or 'default' means the default template:
$template = get_page_template_slug( $page_id );
if ( '' === $template || 'default' === $template ) {
// The page uses the default template.
}
Sources: