在页面上显示特定 WooCommerce 产品属性的术语列表

Display for a specific WooCommerce product attribute the terms list on a page

在 Woocommerce 中,我正在尝试创建一个简单的页面,其中包含我在产品属性 pa_brand 中拥有的所有品牌。但是我没有找到正确的方法。

所以此页面需要将链接的品牌名称显示为列表。

这是一个自定义短代码函数,它将输出 "pa_brand" 产品属性的链接术语名称列表。它可以在任何页面或 post 使用 Wordpress 内容文本编辑器或在 php 代码中使用:

add_shortcode( 'product_attribute_list', 'shortcode_product_attribute_list' );
function shortcode_product_attribute_list( $atts ) {
    // Shortcode Attributes
    $atts = shortcode_atts( array(
        'taxonomy'    => 'pa_brand',
        'orderby'     => 'name',
        'hide_empty'  => false, 
    ), $atts, 'product_attribute_list' );

    $terms = get_terms( array(
        'taxonomy'      => $atts['taxonomy'],
        'orderby'       => $atts['orderby'],
        'hide_empty'    => $atts['hide_empty'], 
    ) );

    $html = '<ul class="' . $atts['taxonomy'] . ' brands">';

    // Loop through the taxonomy terms
    foreach( $terms as $term ){
        $html .= '<li><a href="' . get_term_link( $term, $atts['taxonomy'] ) . '">' . $term->name . '</a></li>';
    }

    return $html . '</ul>'; // Return the output
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

用法示例:

1) 在页面或 post 内容文本编辑器中 (或文本小部件):

[product_attribute_list]

2) 在 php 模板或代码上:

echo do_shortcode("[product_attribute_list]");

您只需将一些 CSS 规则添加到活动主题的 styles.css 文件中即可获得所需的设计。