Php 在单个产品页面中显示特定产品标签的自定义内容

Php 在单个产品页面中显示特定产品标签的自定义内容,php,wordpress,woocommerce,product,taxonomy-terms,Php,Wordpress,Woocommerce,Product,Taxonomy Terms,在简短描述之后,我使用以下内容在WooCommerce单一产品页面中添加自定义html或文本: function my_content( $content ) { $content .= '<div class="custom_content">Custom content!</div>'; return $content; } add_filter('woocommerce_short_description', 'my_conte

在简短描述之后,我使用以下内容在WooCommerce单一产品页面中添加自定义html或文本:

function my_content( $content ) {
    $content .= '<div class="custom_content">Custom content!</div>';

    return $content;
}
add_filter('woocommerce_short_description', 'my_content', 10, 2);
函数我的内容($content){
$content.=“自定义内容!”;
返回$content;
}
添加过滤器('woocommerce\u short\u description'、'my\u content',10,2);
如何在包含特定产品标签(而非产品类别)的产品页面上限制此选项?

您可以使用,将自定义内容限制为仅显示具有特定产品标签的产品,如下所示(在下面的函数中定义产品标签术语):

add_filter('woocommerce_short_description','my_content',10,2);
函数我的内容($content){
//这里定义您的特定产品标签术语(可以是ID、slug或名称)
$product_tags=数组(‘花园’、‘厨房’);
如果(有\项($product\ U tags,'product\ U tag',获取\ ID()){
$content.=''。u uu(“自定义文本!”,'')。';
}
返回$content;
}

代码进入活动子主题(或活动主题)的functions.php文件。它应该能工作。

它工作得很好。非常感谢。大家好,不幸的是,文本甚至出现在列出包含该标签的产品的分类页面上:(。有没有办法将其限制为仅限于产品页面?@Clawdududa use:
if(Is_product()&&has_term($product_tags,'product_tag',get_the_ID()){
add_filter('woocommerce_short_description', 'my_content', 10, 2);
function my_content( $content ) {
    // Here define your specific product tag terms (can be Ids, slugs or names)
    $product_tags = array( 'Garden', 'Kitchen' );

    if( has_term( $product_tags, 'product_tag', get_the_ID() ) ) {
        $content .= '<div class="custom_content">' . __("Custom text!", "") . '</div>';
    }
    return $content;
}