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

WooCommerce分类标题后添加专属分类描述问题求助

解决WooCommerce单个产品页面分类描述重复问题

问题根源

你的代码存在两个核心问题:

  • 遍历产品所属的所有分类并输出每个分类的描述,导致多个描述叠加或显示混乱,视觉上呈现为所有分类共用同一个描述。
  • has_term($term->term_id, 'product_cat')判断完全多余——get_the_terms()已经返回了当前产品的关联分类,该判断永远为真,只会重复执行输出逻辑。

解决方案

推荐使用WooCommerce钩子修改分类输出逻辑(避免直接修改模板文件,防止主题更新丢失代码),实现每个分类标题后对应显示自身的短描述:

方法1:通过钩子过滤分类列表输出

将以下代码添加到主题的functions.php文件中:

add_filter('woocommerce_get_product_category_list', 'append_category_description_to_term', 10, 3);
function append_category_description_to_term($list, $product_id, $args) {
    $terms = get_the_terms($product_id, 'product_cat');
    if (!$terms || is_wp_error($terms)) return $list;

    $updated_list = '';
    foreach ($terms as $term) {
        $term_link = get_term_link($term);
        if (is_wp_error($term_link)) continue;

        // 获取当前分类的短描述
        $term_desc = term_description($term->term_id, 'product_cat');
        $desc_html = !empty($term_desc) ? '<div class="category-desc">' . wpautop($term_desc) . '</div>' : '';

        // 拼接分类链接 + 对应描述
        $updated_list .= sprintf(
            '<span class="product-cat-item">%s<a href="%s" rel="tag">%s</a>%s</span>',
            $args['before'],
            esc_url($term_link),
            esc_html($term->name),
            $desc_html . $args['after']
        );
    }

    return $updated_list;
}

方法2:直接修改模板文件(不推荐)

如果你坚持修改content-single-product.php,找到输出产品分类的代码行(通常是调用wc_get_product_category_list()的部分),替换为以下代码:

global $product;
$terms = get_the_terms($product->get_id(), 'product_cat');

if ($terms && !is_wp_error($terms)) {
    echo '<span class="posted_in">' . _n( 'Category:', 'Categories:', count($terms), 'woocommerce' ) . ' ';
    foreach ($terms as $index => $term) {
        $term_link = get_term_link($term);
        if (is_wp_error($term_link)) continue;

        $term_desc = term_description($term->term_id, 'product_cat');
        $desc_html = !empty($term_desc) ? '<div class="category-desc">' . wpautop($term_desc) . '</div>' : '';

        echo sprintf('<a href="%s" rel="tag">%s</a>%s', esc_url($term_link), esc_html($term->name), $desc_html);
        if ($index !== count($terms) - 1) echo ', ';
    }
    echo '</span>';
}

样式调整

添加CSS代码到主题自定义样式中,让描述显示在分类标题下方:

.category-desc {
    margin: 6px 0 12px;
    font-size: 0.9rem;
    color: #767676;
    line-height: 1.4;
}

其他实现思路

  • 若仅需显示主分类的描述:在遍历分类时,判断$term->parent === 0(主分类标识),只输出主分类的描述。
  • 若要自定义描述显示位置:使用woocommerce_single_product_summary钩子,指定优先级插入描述内容,比如:
add_action('woocommerce_single_product_summary', 'display_main_category_description', 25);
function display_main_category_description() {
    global $product;
    $terms = get_the_terms($product->get_id(), 'product_cat');
    if (!$terms || is_wp_error($terms)) return;

    // 找到第一个主分类,若无主分类则取第一个分类
    $main_term = current(array_filter($terms, function($t) { return $t->parent === 0; }));
    if (!$main_term) $main_term = reset($terms);

    $desc = term_description($main_term->term_id, 'product_cat');
    if (!empty($desc)) {
        echo '<div class="main-category-desc"><h4>' . esc_html($main_term->name) . '</h4>' . wpautop($desc) . '</div>';
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 10:56:14