6

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

  • is_page_template() is the correct function but you say it "doesn't work". Are you checking inside the loop? – jdm2112 Jun 16 '14 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. – user1981248 Jun 17 '14 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 Jun 17 '14 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 Jun 17 '14 at 7:47
8

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 I then check if it is equal to 'page.php'

  • 1
    May you please elaborate more on how this code will work? – Ethan Jinks O'Sullivan Sep 20 '16 at 15:29
  • 1
    I agree, this is a solid solution but could be expanded upon.+1 – Howdy_McGee Sep 20 '16 at 16:48
  • At least for me, this return true for any page, even if use a custom template (different than page.php) – Jabel Márquez Sep 26 '17 at 3:20
  • 1
    This is awesome because it works even if the page is using its default template. – rinogo Aug 9 '18 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 ... – Friedrich Siever Aug 28 '18 at 20:41
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.

  • +1 Any code needing this condition could probably just be placed unconditionally within page.php – Andy Macaulay-Brook Sep 20 '16 at 17:16
  • 1
    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. – Jordan Carter Jul 26 '18 at 18:55
1

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

  • Wouldn't this return false on post pages, though? – Jordan Carter Jul 26 '18 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');
    }
-2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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