在简短描述后,我使用以下内容在 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/