php - 显示特定 WooCommerce 产品的属性页面上的术语列表
问题描述
在 Woocommerce 中,我正在尝试创建一个简单的页面,其中包含我从产品属性中拥有的所有品牌pa_brand
。但是我没有找到正确的方法来做到这一点。
所以这个页面需要将链接的品牌名称显示为列表。
解决方案
这是一个自定义简码函数,它将输出“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 文件即可获得所需的设计。
推荐阅读
- r - 使用分类数据而不是连接线创建线图
- javascript - 如何从祖先覆盖嵌套 Material UI 组件的样式?
- apache-kafka - 如何连接到内部 Kafka 集群
- r - 如何在 Rselenium R 中捕获动态 xpath id
- php - LeagueCSV:getRecords() 上的未定义索引
- pthreads - 在 C 中使用 pthread_mutex 同步分叉进程
- javascript - 未调用 jsgrid 控制器 updateItem
- excel - 从一个工作簿复制随机数据并将其粘贴到另一工作簿的同一行但不同列中
- c++ - 如何将对象的构造限制为几种方法?
- localhost - 是否可以在本地系统(非容器)上安装 Tekton