You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce如何在产品长描述为空时隐藏"Description"标题

问题原因

你之前使用的woocommerce_product_tabs钩子仅作用于WooCommerce默认的标签式描述模块,你当前使用的主题已经将描述区块单独抽离自定义渲染,所以原有代码无法生效。

可行解决方案

方案1:PHP代码实现(推荐,无页面闪烁)

将以下代码添加到你当前主题/子主题的functions.php文件中,或使用代码片段插件插入即可生效:

// 产品长描述为空时隐藏自定义描述区块
add_action('wp_head', 'hide_empty_custom_product_description');
function hide_empty_custom_product_description() {
    // 仅在产品详情页执行逻辑
    if (!is_product()) return;
    global $post;
    // 渲染并获取产品长描述的实际内容
    $product_full_description = apply_filters('the_content', $post->post_content);
    // 去除标签和所有空白字符后判断是否为空
    if (trim(strip_tags($product_full_description)) === '') {
        echo '<style>.product-description { display: none !important; }</style>';
    }
}

方案2:前端JS实现(备用,适合无法修改PHP代码的场景)

将以下代码添加到主题自定义设置的自定义JS区域,或页面底部的自定义代码块中:

document.addEventListener('DOMContentLoaded', function() {
    // 仅在产品页执行
    if (!document.body.classList.contains('single-product')) return;
    const descriptionBlock = document.querySelector('.product-description');
    if (!descriptionBlock) return;
    // 获取描述区块的实际文本内容,判断是否为空
    const descriptionText = descriptionBlock.textContent.trim();
    if (descriptionText === '') {
        descriptionBlock.style.display = 'none';
    }
});

内容的提问来源于stack exchange,提问作者user3169905

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 11:45:08