Wordpress Woocommerce 在商店页面上显示属性

我想给wordpress的商店页面添加一些属性。我在 Stackoverflow 上找到的这段代码显示了所有属性标签,但在所有相同的属性名称上。


add_action('woocommerce_after_shop_loop_item_title','add_attribute');

function add_attribute() {

global $product;


$product_attributes = array( 'pa_country','pa_class','pa_faction','pa_gender' );

$attr_output = array();



foreach( $product_attributes as $taxonomy ){

    if( taxonomy_exists($taxonomy) ){

        $label_name = get_taxonomy( $taxonomy )->labels->singular_name;

        $value = $product->get_attribute('pa_country','pa_class','pa_faction','pa_gender');


        if( ! empty($value) ){


            $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';

        }

    }}



echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';

}

当前状态

我只需要一点帮助就可以让它显示所有正确的属性。


神不在的星期二
浏览 196回答 1
1回答

倚天杖

您的代码中有一些小错误。请尝试以下操作:add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');function display_shop_loop_product_attributes() {&nbsp; &nbsp; global $product;&nbsp; &nbsp; // Define you product attribute taxonomies in the array&nbsp; &nbsp; $product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );&nbsp; &nbsp; $attr_output = array(); // Initializing&nbsp; &nbsp; // Loop through your defined product attribute taxonomies&nbsp; &nbsp; foreach( $product_attribute_taxonomies as $taxonomy ){&nbsp; &nbsp; &nbsp; &nbsp; if( taxonomy_exists($taxonomy) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $label_name = wc_attribute_label( $taxonomy, $product );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $term_names = $product->get_attribute( $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( ! empty($term_names) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Output&nbsp; &nbsp; echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';}代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。仅对于简单的产品,您将使用以下内容:add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');function display_shop_loop_product_attributes() {&nbsp; &nbsp; global $product;&nbsp; &nbsp; // Only for simple products&nbsp; &nbsp; if ( ! $product->is_type( 'simple' ) ) return;&nbsp; &nbsp; // Define you product attribute taxonomies in the array&nbsp; &nbsp; $product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );&nbsp; &nbsp; $attr_output = array(); // Initializing&nbsp; &nbsp; // Loop through your defined product attribute taxonomies&nbsp; &nbsp; foreach( $product_attribute_taxonomies as $taxonomy ){&nbsp; &nbsp; &nbsp; &nbsp; if( taxonomy_exists($taxonomy) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $label_name = wc_attribute_label( $taxonomy, $product );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $term_names = $product->get_attribute( $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( ! empty($term_names) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Output&nbsp; &nbsp; echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';}代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP