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

如何在Woo-Product-Carousel中显示商品标签

解决Woo商品滑块价格下方添加商品标签的问题

你当前代码中尝试添加标签的部分是错误的,Helper::eael_validate_html_tag( $settings['eael_product_carousel_title_tag'] ) 只会输出标题标签(如h3、h4)的标签名,而非商品的实际标签。以下是正确的修改方案:

核心修改步骤

在价格输出的if($should_print_price )代码块中,替换你错误的标签输出代码,改用WooCommerce内置函数获取并输出商品标签:

修改后的完整代码片段

<div class="product-details-wrap">
    <?php
    if ( $settings['eael_product_carousel_show_title'] ) {
        echo '<div class="eael-product-title">';
        if( $should_print_title_clickable ) {
            echo '<a href="' . $product->get_permalink() . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
        }
        echo '<' . Helper::eael_validate_html_tag( $settings['eael_product_carousel_title_tag'] ) . '>';
        if ( empty( $settings['eael_product_carousel_title_length'] ) ) {
            echo Helper::eael_wp_kses( $product->get_title() );
        } else {
            echo implode( " ", array_slice( explode( " ", Helper::eael_wp_kses($product->get_title()) ), 0, $settings['eael_product_carousel_title_length'] ) );
        }
        echo '</' . Helper::eael_validate_html_tag( $settings['eael_product_carousel_title_tag'] ) . '>';
        if( $should_print_title_clickable ) {
            echo '</a>';
        }
        echo '</div>';
    }
    ?>
    <?php if($should_print_price ){
        echo '<div class="eael-product-price">'.$product->get_price_html().'</div>';
        
        // 新增:获取并输出商品标签
        $product_tags = get_the_terms( $product->get_id(), 'product_tag' );
        if ( !empty( $product_tags ) && !is_wp_error( $product_tags ) ) {
            echo '<div class="eael-product-tags">';
            foreach ( $product_tags as $tag ) {
                echo '<span class="eael-product-tag">' . esc_html( $tag->name ) . '</span>';
            }
            echo '</div>';
        }
        
    }?>
    <?php if ($should_print_rating) {
        echo wc_get_rating_html
        ($product->get_average_rating(), $product->get_rating_count());
    }
    if ( $should_print_excerpt ) {
        echo '<div class="eael-product-excerpt">';
        echo '<p>' . wp_trim_words(strip_shortcodes(get_the_excerpt()), $settings['eael_product_carousel_excerpt_length'],
                $settings['eael_product_carousel_excerpt_expanison_indicator']) . '</p>';
        echo '</div>';
    }
    ?>
</div>

补充说明

  1. 标签获取逻辑:使用get_the_terms()函数获取当前商品的所有product_tag分类(WooCommerce商品标签的默认分类标识)。
  2. 容错处理:添加空值与错误判断,确保只有存在有效标签时才输出标签容器,避免页面报错。
  3. 样式自定义:每个标签用<span class="eael-product-tag">包裹,你可以通过自定义CSS调整样式,示例:
.eael-product-tags {
    margin-top: 8px;
}
.eael-product-tag {
    display: inline-block;
    padding: 2px 8px;
    background: #f0f0f0;
    border-radius: 4px;
    font-size: 12px;
    margin-right: 4px;
    margin-bottom: 4px;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 05:50:30