我想在分类页面上展示我的产品的两个属性,即加热能力和冷却能力,因为我在woocommerce制作一个在线商店,我正在使用我的定制主题。它不是一个变体或什么的,它只是一个文本的普通属性,我想显示类似于这的东西,因为您可以看到类别页面上列出的加热和冷却系统,我试过了代码
if (!function_exists('shop_attributes_in_loop')) {
function shop_attributes_in_loop(){
global $product;
$attributes = $product->get_attributes();
if(!empty($attributes)){
$attribute_single = array_keys($attributes);
$myArray = array();
echo '<div class="product_attributes">';
foreach ($attribute_single as $attribute => $value) {
$myArray[] = ucfirst($value);
}
echo implode(', ', $myArray).'</div>';
}
}
}
add_action('woocommerce_after_shop_loop_item', 'shop_attributes_in_loop');但它展示了一些东西,如户外单元、安装、Pa_模型-非室内、Pa_模型-无室外等。有人能帮我吗..。

发布于 2016-07-11 11:17:49
实际上,我发现了该如何做:)
add_action( 'woocommerce_after_shop_loop_item', 'custom_display_post_meta', 9 );
function custom_display_post_meta() {
global $product;
$attr = array('pa_cooling-capacity-watts', 'pa_heating-capacity-watts');
foreach ( $attr as $attribute ) {
$values = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'names' ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
}发布于 2016-07-11 09:50:21
您可以使用此片段在存档/类别页获取自定义字段值:
add_action( 'woocommerce_after_shop_loop_item', 'custom_display_post_meta', 9 );
function custom_display_post_meta() {
global $product;
$heating_capacity = get_post_meta( $product->id, 'heating_capacity', true );
$cooling_capacity = get_post_meta( $product->id, 'cooling_capacity', true );
echo $heating_capacity;
echo $cooling_capacity;
}您可以将本文作为参考阅读:https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
https://stackoverflow.com/questions/38300940
复制相似问题