php - 在特定的 Woocommerce 产品类别文件页面上显示产品属性

标签 php wordpress woocommerce product custom-taxonomy

我想在类别页面上显示两个属性,仅在特定类别上显示属性名称和值。

我发现的这段代码显示了属性的标签,但复制了值,我真的很难显示类别变量。任何帮助是极大的赞赏。

screenshot

代码:

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>'; 
}

最佳答案

更新 - 问题来自以下行,其中产品属性值始终针对相同的产品属性:

$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 文件中。经过测试并有效。

enter image description here

<小时/>

定位产品类别存档页面:

您将使用the conditional tag is_product_category()函数内部的 IF 语句...

对于特定的产品类别归档页面,可以设置as explained here在数组中的函数内部,例如:

if( is_product_category( array('chairs', 'beds') ) {
    // Here go the code to be displayed
}

您只需在数组中设置正确的产品类别名称...

<小时/>

相关:Show WooCommerce product attributes in custom home and product category archives

关于php - 在特定的 Woocommerce 产品类别文件页面上显示产品属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53510707/

相关文章:

php - 在 MySQL 中插入多个 JSON 列(使用 API)

php - PayPal 快速结帐页面中不允许使用非英语字符

html - 为什么 WordPress 向我的 &lt;input&gt; 标签(空元素)添加结束标签?

css - 修改wordpress中元素的CSS

php - 如何自定义 woocommerce "My Account"页面?

php - 加载页面时忽略 html 标签

php - 在 PHP 中使用 XMLReader 读取 XML 和验证模式时出错

wordpress - 通过 XML RPC 获取 slug 的帖子

html - 如何降低背景渐变的高度?

WordPress 以主题风格显示自定义菜单