7

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

11
  • 1
    Where do you use this code? Commented Feb 11, 2019 at 15:14
  • 1
    is page-47.php in directory ? Not in the theme root. If so you need to is_page_template( 'templates/page-47.php' ); Commented Feb 11, 2019 at 15:14
  • Hi page-47.php is inside main theme folder. So i using is_page_template( 'page-47.php' ). But not working. Commented Feb 11, 2019 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
    Commented Feb 11, 2019 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
    Commented Feb 11, 2019 at 17:21

2 Answers 2

18

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

}
2
  • 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 { } ?> Commented Feb 12, 2019 at 10:09
  • My answer includes your example... Commented Feb 12, 2019 at 10:13
1

Thanks to Jacob Peattie! This also solved my problem.

add_filter( 'woocommerce_currency_symbol', 'change_currency_symbol', 10, 2 );

function change_currency_symbol( $symbols, $currency ) {
    
    global $template;
    
    if ( basename( $template ) === 'archive-chiptuning.php' && ( 'EUR' === $currency )) {
        return '';
    }
        return $symbols;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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