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

标签 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);

如何在包含特定产品标签(而非产品类别)的产品页面上限制此操作??

最佳答案

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

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 文件中。它应该有效。

关于php - 在 Woocommerce 单一产品页面中显示特定产品标签的自定义内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66284818/

相关文章:

php - While 循环与计数查询? (PHP/MySQL)

php - DataTables dataTables_wrapper 未加载

php - 需要在文件夹中找到的所有 block (而不是将它们全部写出)

wordpress - 根据运输方式使用 woocommerce 删除选定的结帐字段

php - 在 WooCommerce 文件中的产品行之间添加内容

php - 如何刷新wordpress变体缩略图?

php - CSV 导出 : Multiple Database Rows to Single Row

javascript - preventDefault() 后未正确发送 POST 数据 - 新 Google 的隐形 reCAPTCHA 相关

php - jQuery:通过单击问题切换在 php 中生成的问答列表中答案的可见性

php - 如何动态更改联系表 7 重定向 URL - WordPress