11

I'm trying to check whether the page.php template is being used or not. I've tried is_page_template( 'page.php' ) but it doesn't work.

I also can't use is_page() because I only want to do something when page.php is used, and not when some other custom Page template is used.

Please let me know about this and thanks!

Edit: Should have mentioned earlier that I have a file called common-options.php which is getting included in almost all the templates (like index.php, page.php, single.php and page_blog.php), in this file I'm trying to do the check with the following code:

if ( is_page_template( 'page.php' ) ) {
    echo "success!";
}

But it's not working when I open a page that is using the "Default Template".

4
  • is_page_template() is the correct function but you say it "doesn't work". Are you checking inside the loop?
    – jdm2112
    Commented Jun 16, 2014 at 22:15
  • Hi, I'm checking it in a file which I'm including (using 'include()') at the top of page.php and other files. Please see the edit of my question. Commented Jun 17, 2014 at 0:40
  • Unfortunately I'm not able to answer my own question from your edit. Too many possibilities without seeing more of your code. If you are testing the condition inside of the loop, is_page_template will not work. My recommendation is to find out which template is being use and then track down why. This function will display the current template file on every page to a logged in admin. pastebin.com/wkNv9Eug. Stick this in your functions.php and reload the page. As long as your theme calls wp_head() you should see the template at the top of every page when logged in.
    – jdm2112
    Commented Jun 17, 2014 at 2:58
  • 1
    The most important question here is: are you checking inside the loop? Are you calling the_post in your template file before calling is_page_template? (is_page_template( 'page.php' won't work either way, but this is still an important question in answering your original question)
    – engelen
    Commented Jun 17, 2014 at 7:47

7 Answers 7

12

I actually ran into the same problem myself and my solution was this code:

if(basename(get_page_template()) === 'page.php'){

}

basename(get_page_template()) gets the filename of the page template (according to https://codex.wordpress.org/Function_Reference/get_page_template) and we then check if it is equal to 'page.php'

5
  • 1
    May you please elaborate more on how this code will work? Commented Sep 20, 2016 at 15:29
  • 1
    I agree, this is a solid solution but could be expanded upon.+1
    – Howdy_McGee
    Commented Sep 20, 2016 at 16:48
  • At least for me, this return true for any page, even if use a custom template (different than page.php) Commented Sep 26, 2017 at 3:20
  • 1
    This is awesome because it works even if the page is using its default template.
    – rinogo
    Commented Aug 9, 2018 at 23:10
  • 1
    Works wonderful for me in functions.php with wp enqueue hook. is_page_template( 'page.php' ) didn't fire ... . Wonder if someone would find an explaination ... Commented Aug 28, 2018 at 20:41
7

You have two useful functions: is_page() and is_page_template().

The is_page() function will return true when you're on a page, and the is_page_template() function will return false if the current post (can be a page) is not using a custom template.

So, you just need to use both of these functions together:

if ( is_page() && !is_page_template() ) {
  // your code
}
2
  • 1
    This is the most robust answer I think. Commented Nov 17, 2020 at 16:42
  • 1
    Still works perfect as of this date. Thank you!
    – rwzdoorn
    Commented Jan 12, 2021 at 16:12
1

Old question, but an interesting one.

In general it is uncommon to check for template in WordPress. The template is result of main query running and setting up context of request.

If necessary I would probably somehow set a flag at start of page.php, such as declaring constant for it, to have my own context for that.

Overall though it makes me think that any logic that requires such move should be considered to be adjusted in line with more common mechanics and conditionals.

2
  • +1 Any code needing this condition could probably just be placed unconditionally within page.php Commented Sep 20, 2016 at 17:16
  • 3
    I disagree. For me, I am trying to enqueue a JS script only on the default page.php, inside of functions.php. Why make the user load it on any other page if they don't need to? I do think there are use cases for this. Commented Jul 26, 2018 at 18:55
0

Actually it is very simple. Just use:

is_page_template()

If it returns true than page.php is not being used and vice versa :)

1
  • Wouldn't this return false on post pages, though? Commented Jul 26, 2018 at 18:57
0
$bulkitnt_custom_page_control = is_page_template( 'custom-page.php' );

    if( $bulkitnt_custom_page_control != true ){
        do_action('bulkitnt_header_action');
    }
0

This works for me in the seachbar.php

basename(get_page_template()) === ( 'page.php' )

This my compleet code:

if ( is_page_template( 'tpl-ads-home.php' ) || basename(get_page_template()) === ( 'page.php' ) || is_page_template( 'tpl-refine-search.php' ) || is_page_template( 'tpl-categories.php' ) || is_search() || is_404() || is_tax( APP_TAX_CAT ) || is_tax( APP_TAX_TAG ) || is_singular( APP_POST_TYPE ) ) :

    $args = cp_get_dropdown_categories_search_args( 'bar' );
2
  • Compleet code <?php if ( is_page_template( 'tpl-ads-home.php' ) || basename(get_page_template()) === ( 'page.php' ) || is_page_template( 'tpl-refine-search.php' ) || is_page_template( 'tpl-categories.php' ) || is_search() || is_404() || is_tax( APP_TAX_CAT ) || is_tax( APP_TAX_TAG ) || is_singular( APP_POST_TYPE ) ) : $args = cp_get_dropdown_categories_search_args( 'bar' ); ?>
    – Robin
    Commented Jul 27, 2020 at 15:15
  • You should add your code to your answer, not as a comment
    – mozboz
    Commented Jul 27, 2020 at 15:26
-2

Just use null array instead of 'page.php' eg: is_page_template(array(''));

2
  • Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Commented Feb 1, 2019 at 13:48
  • You could also provide an example. Commented Feb 1, 2019 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.