Updated- 问题来自以下行,其中产品属性属性值始终针对相同的产品属性:
$value = $product->get_attribute( 'pa_set' );
应该是这样的:
$value = $product->get_attribute( $taxonomy );
完整的重新访问代码将是:
add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' );
function display_loop_product_attribute() {
global $product;
$product_attributes = array('pa_set', 'pa_team'); // Defined product attribute taxonomies.
$attr_output = array(); // Initializing
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
if( $value = $product->get_attribute($taxonomy) ){
// The product attribute label name
$label_name = wc_attribute_label($taxonomy);
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes">'.implode('<br>', $attr_output).'</div>';
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
定位产品类别存档页面:
你将使用条件标签is_product_category() https://docs.woocommerce.com/document/conditional-tags/#section-5在函数内部IF
陈述…
对于特定的产品类别归档页面,您可以设置它们正如这里所解释的 https://docs.woocommerce.com/document/conditional-tags/#section-5在数组中的函数内部,例如:
if( is_product_category( array('chairs', 'beds') ) {
// Here go the code to be displayed
}
您只需要在数组中设置正确的产品类别...
有关的:在自定义主页和产品类别档案中显示 WooCommerce 产品属性 https://stackoverflow.com/questions/55129529/show-woocommerce-product-attributes-in-custom-home-and-product-category-archives/55131199#55131199