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

时间:2021-02-19 20:49:57

标签: 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_content', 10, 2);

如何在包含特定产品标签(不是产品类别)的产品页面上进行限制?

1 个答案:

答案 0 :(得分:2)

您可以使用 has_term() WordPress conditional function 来限制您的自定义内容仅显示具有特定产品标签的产品,如下(在下面的功能中定义您的产品标签术语( s))

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;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该有效。