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 |7 Answers
Reset to default 12I 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(''));
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:58the_post
in your template file before callingis_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