首页 > 解决方案 > 显示特定 WooCommerce 产品的属性页面上的术语列表

问题描述

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

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

标签: phphtmlwordpresswoocommercetaxonomy-terms

解决方案


这是一个自定义简码函数,它将输出“pa_brand”产品属性的链接术语名称列表。它可以使用 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)在页面或帖子内容文本编辑器(或文本小部件)内:

[product_attribute_list]

2) 在 php 模板或代码上:

echo do_shortcode("[product_attribute_list]");

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


推荐阅读