Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1279174
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI try to require_once()
a file, if the condition of a specific tag is met, on a woocommerce single product page.
I've added following to my functions.php
if(has_term('xyz', 'product_tag')){
require_once WP_CONTENT_DIR . '/path/file.php';
}
It doesn't load my file, but if I take it out of my condition, it does load.
What can I do to achieve this logic?
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI try to require_once()
a file, if the condition of a specific tag is met, on a woocommerce single product page.
I've added following to my functions.php
if(has_term('xyz', 'product_tag')){
require_once WP_CONTENT_DIR . '/path/file.php';
}
It doesn't load my file, but if I take it out of my condition, it does load.
What can I do to achieve this logic?
Share Improve this question edited Oct 14, 2021 at 20:07 vancoder 7,92428 silver badges35 bronze badges asked Oct 14, 2021 at 19:05 JannicklaJannickla 176 bronze badges2 Answers
Reset to default 1has_term()
is a template function therefore it does not work if you use it straight in functions.php which is loaded well before knowing if the current object is a product page.
So if you use it in functions.php
you could do:
add_action('template_redirect','my_inclusion_function');
function my_inclusion_function(){
if(is_product() && has_term('xyz', 'product_tag')){
require_once( WP_CONTENT_DIR.'/path/file.php');
}
}
is_product()
checks if you are in a single product and then if the product has tag xyz
If the inclusion has layout implications such as print additional information or so, then this approach is not ideal since the inclusion occurs at the very beginning of the content jsut after the <body>
tag opening. In this case you can just modify the single-product.php template file (as suggested in the comment by tiago calado)
Also you can use one of the available hooks in the single-product page check this useful resource and use it directly in functions.php. The advantage of this last approach is that you don't have to review the layout pages in case of future theme switch.
require_once is a function, try this way.
if(has_term('xyz', 'product_tag')){
require_once( WP_CONTENT_DIR.'/path/file.php');
}
also require_once() in functions.php, dont know what to say. give feedback if the code above had work has intended.
本文标签: phprequireonce() if a product in woocommerce contains a tag
版权声明:本文标题:php - require_once() if a product in woocommerce contains a tag 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268644a2368909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论