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

page template - is_page_template not working

programmeradmin0浏览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

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

Share Improve this question edited Feb 11, 2019 at 16:51 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 11, 2019 at 15:10 soliddigitalsoliddigital 1731 gold badge1 silver badge6 bronze badges 11
  • 1 Where do you use this code? – Krzysiek Dróżdż 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' ); – Liam Stewart 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. – soliddigital 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
 |  Show 6 more comments

2 Answers 2

Reset to default 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' ) {

}

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;
}
发布评论

评论列表(0)

  1. 暂无评论