WordPress.org

Support

Support » How-To and Troubleshooting » is_page_template refuses to acknowledge the default page template

is_page_template refuses to acknowledge the default page template

  • This is driving me insane.

    if (is_page_template(‘page.php’)) never returns true for me, whether a page is using page.php or not. Systematic research gives the following situations:

    Page uses template of page.php
    if (is_page_template(‘page.php’)) : FALSE
    if (!is_page_template(‘page.php’)) : TRUE

    Page uses template of page2.php
    if (is_page_template(‘page2.php’)) : TRUE
    if (!is_page_template(‘page2.php’)) : FALSE

    Why is this happening? Is is_page_template supposed to ignore page.php? If so, why? Is there some other easy way of testing for the page.php template?

Viewing 8 replies - 1 through 8 (of 8 total)
  • As a sanity check, I have the following code, and yes, it’s before the loop (note that as mentioned above, everything works fine when the template is page2.php):

    echo get_page_template().'<br/>'; 
    
    if (is_page_template('page.php')) {
    echo 'this is page.php';
     }
     else {
    echo 'this is definitely NOT page.php';
    }

    When viewing a page that uses page.php, I get the following output:

    [theme path]/page.php
    this is definitely NOT page.php

    I mean… what?

    Hey there,

    I had the same issue as you but figured this simple workaround will work for my needs:

    $pageTemplate = get_page_template();
    
    $pageArray = explode("/", $pageTemplate); 
    
    $pageTemplate = end($pageArray);
    
    	if($pageTemplate == 'page.php') :
    		//do something
    	endif;

    Let me know if this works for you.

    I’ve been hitting the same wall with is_page_template() and the default page template. To my mind it seems inconsistent – maybe it’s a bug? Maybe there’s a logical reason why it behaves this way but I haven’t poked into the WP code to see if maybe this is deliberate.

    Thanks for the workaround Devin… yeah processing get_page_template() will solve the problem for me too.

    Glad to see other people have hit the same issue as me so I’ll stop poking it & assuming it was me at fault and I’ll just work around it 🙂

    Is page template refers to… is a page template being used.

    page.php is not a page template per se, it is the default template for pages.

    Kind of confusing, yes…. but any custom page template makes is_page_template true, its a test for the usage of a page template

    YOu can see it differentiated in the body class of a page

    A page using a page template will load these body classes:
    page-template page-template-templatename-php

    and a page without (using page.php) loads up just
    page-template-default

    letting you know it’s using the default template, but not a page-template….

    PS To save the overhead of exploding the array, I did

    !strcmp($myTemplate, substr(strrchr(get_page_template(), '/'),1))

    as a drop-in replacement for is_page_template($myTemplate)

    Ahhhh yes Rev! I spotted that… the difference between the body classes including ‘page-template-default’ rather than ‘page.php’ and it made me think maybe there was logic at work but I didn’t think it all the way through.

    Good explanation… it ISN’T using a page template in so far as there is no special named template for this page… it’s had to fallback to the default.

    So WordPress is working correctly and being consistent. But the original requirement still exists… I guess the question now becomes “how do I test to see if my page is using the default page template?”

    Maybe (is_page() && !is_page_template()) would do the trick.

    Thanks again for your explanation.

    That seems like a good way to do it.

    (is_page() && !is_page_template())

    WORKS PERFECTLY.

    THANK YOU!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘is_page_template refuses to acknowledge the default page template’ is closed to new replies.
Skip to toolbar