首页 > 解决方案 > 商店页面上的额外产品信息

问题描述

如何在商店页面列表的产品名称下显示单个产品的类别?

店铺页面截图:

在此处输入图像描述

标签: wordpresswoocommerceproductshopwoocommerce-theming

解决方案


您应该尝试在每个产品的价格上方显示产品类别。

add_filter( 'woocommerce_get_price_html', 'woo_shipping_with_price_display' );
add_filter( 'woocommerce_cart_item_price', 'woo_shipping_with_price_display' );

function woo_shipping_with_price_display( $price ) {
    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
    $text = '';
    if ( $product_cats && ! is_wp_error ( $product_cats ) ){
        $single_cat = array_shift( $product_cats );
        $text = '<div itemprop="name" class="product_category_title"><span>'.__(esc_attr($single_cat->name)).'</span></div>';
        
    }
    
    if ($price  == true) {
        return $text . $price;
    }
    else {
        return $text;
    }
}

您可以从以下行更改/更新 html、class 等:

$text = '<div itemprop="name" class="product_category_title"><span>'.__(esc_attr($single_cat->name)).'</span></div>';

推荐阅读