0

Hi trying this code below to get working, but no look so far. Idea is simple if page template is page-47.php display <h1>Something</h1> else <h1>This will show on any other page</h1> .

<?php if ( is_page_template( 'page-47.php' ) ): ?>

   <h1>Something</h1>

<?php else: ?>

   <h1>This will show on any other page</h1>

<?php endif ?>

Thank You

  • 1
    Where do you use this code? – Krzysiek Dróżdż Feb 11 at 15:14
  • is page-47.php in directory ? Not in the theme root. If so you need to is_page_template( 'templates/page-47.php' ); – Liam Stewart Feb 11 at 15:14
  • Hi page-47.php is inside main theme folder. So i using is_page_template( 'page-47.php' ). But not working. – Deividas Krupstas Feb 11 at 15:17
  • 1
    As Kryzsiek asked - what file is this code actually in? You may be trying to call it somewhere before the query is fully set up. – WebElaine Feb 11 at 15:48
  • 1
    Since this is a stable function that is known to work, there is something in the context of how you are using it that is off. That is why people are asking where exactly you are using it. For instance, if you have written a custom loop on the page and you are using this function within that loop, your results will be different. – tmdesigned Feb 11 at 17:21
0

is_page_template() will only tell you if the page is using a custom page template. Meaning a template that was created by adding the Template Name: comment to the file and selecting it from the Template dropdown, as described here. The function works by checking the post meta for which template was selected.

If you have created a page template using the slug or ID using the method described here, which you appear to have, then the template being used is not stored in meta, so won't be picked up by the is_page_template() function.

If you want to know the filename of the current template being used, regardless of whether it's a custom template or not, you can use the global $template variable:

global $template;

if ( basename( $template ) === 'page-47.php' ) {

}
  • Thank you for explanation!. How can adapt your suggested code in my example? <?php global $template; if ( is_page_template( 'page-47.php' ) ) { echo "Hello world!"; } else { } ?> – Deividas Krupstas Feb 12 at 10:09
  • My answer includes your example... – Jacob Peattie Feb 12 at 10:13

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.