WordPress.org

Ready to get started?Download WordPress

Forums

is_page_template refuses to acknowledge the default page template (9 posts)

  1. fouriertransform
    Member
    Posted 3 years ago #

    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?

  2. fouriertransform
    Member
    Posted 3 years ago #

    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?

  3. Devin Walker
    Member
    Posted 3 years ago #

    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.

  4. Jon Jennings
    Member
    Posted 2 years ago #

    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 :-)

  5. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

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

  6. Jon Jennings
    Member
    Posted 2 years ago #

    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)

  7. Jon Jennings
    Member
    Posted 2 years ago #

    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.

  8. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    That seems like a good way to do it.

  9. Bill Zinclemyer
    Member
    Posted 2 years ago #

    (is_page() && !is_page_template())

    WORKS PERFECTLY.

    THANK YOU!

Topic Closed

This topic has been closed to new replies.

About this Topic