Is there a way to check to see if the page.php template is being used? The standard way isn’t working:
<?php echo is_page_template("page.php") ? "Page being used" : "Page not being used"; ?>
However if I change page.php to any other template (page-about.php etc) it works fine.
Actually it is very simple
Just use:
is_page_template()
If it returns true than page.php is not being used and vice versa
🙂
I could not get
is_page_template();
to work properly either. I put this intofooter.php
andheader.php
to test it and it shows me which page template is being used as well as whetherpage.php
is being used. If it is put in the header it could easily be adapted to control what happens next on the page.It should probably be done properly WordPress style and put into your
functions.php
in your child theme but this is the basic code:It gets the full path of the current template, splits off the page name and compares or echoes it. If you are actually going to echo it and not just use its functionality to modify other display parameters then you should enclose it in
htmlspecialchars();
to avoid inject attack.This looks like a discussion of a more WordPressy way to do it and a possible reason why it was not working – though I would have thought that by the time you get to
footer.php
it should be pretty clear which template was used https://wordpress.org/support/topic/is_page_template-in-functionsphp-not-workingJust for reference this is the WordPress method with comments on its limitations when used inside The Loop. https://developer.wordpress.org/reference/functions/is_page_template/