admin管理员组

文章数量:1279174

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 question

I 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 question

I 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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

has_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