最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Conditional tag to check if 'page.php' is being used?

programmeradmin7浏览0评论

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

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

Share Improve this question edited Jun 17, 2014 at 7:48 engelen 3,3861 gold badge21 silver badges18 bronze badges asked Jun 16, 2014 at 21:40 user1981248user1981248 1,0774 gold badges14 silver badges21 bronze badges 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. – user1981248 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/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
Add a comment  | 

7 Answers 7

Reset to default 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/Function_Reference/get_page_template) and we then check if it is equal to 'page.php'

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
}

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.

Actually it is very simple. Just use:

is_page_template()

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

$bulkitnt_custom_page_control = is_page_template( 'custom-page.php' );

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

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' );

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

发布评论

评论列表(0)

  1. 暂无评论