【发布时间】:2019-04-29 20:16:54
【问题描述】:
我想在类别页面上显示两个属性,属性名称和值仅针对特定类别。
我发现的这段代码显示了属性的标签,但是复制了值,我真的很努力显示类别变量。任何帮助是极大的赞赏。
代码:
add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
global $product;
$product_attributes = array( 'pa_set', 'pa_team');
$attr_output = array();
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
$value = $product->get_attribute('pa_set');
if( ! empty($value) ){
// 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>';
}
【问题讨论】:
标签: php wordpress woocommerce product custom-taxonomy