请求技术支持:在WooCommerce产品归档页显示所有产品属性
在WooCommerce产品归档页显示所有商品属性
你可以将以下代码添加到当前主题的functions.php文件中,或者使用自定义代码插件(如Code Snippets)来实现需求:
// 在产品归档页商品下方显示所有属性 add_action( 'woocommerce_after_shop_loop_item', 'display_all_product_attributes_on_archive', 15 ); function display_all_product_attributes_on_archive() { global $product; // 获取产品的所有属性 $attributes = $product->get_attributes(); if ( ! empty( $attributes ) ) { echo '<div class="product-attributes">'; foreach ( $attributes as $attribute ) { // 跳过用于变体的属性和设置为不可见的属性 if ( $attribute->get_variation() || ! $attribute->get_visible() ) { continue; } $attribute_name = wc_attribute_label( $attribute->get_name() ); $attribute_values = implode( ', ', $attribute->get_options() ); echo '<p class="attribute-item"><strong>' . esc_html( $attribute_name ) . ':</strong> ' . esc_html( $attribute_values ) . '</p>'; } echo '</div>'; } }
说明
- 代码通过
woocommerce_after_shop_loop_item钩子,在每个商品的默认元素之后(优先级15)插入属性列表 - 仅显示设置为“在产品页面上可见”的属性,自动跳过用于产品变体的属性
- 可以通过
.product-attributes和.attribute-item这两个CSS类自定义属性区域的样式
内容的提问来源于stack exchange,提问作者Niles Nimmo
相关产品推荐
相关产品推荐

