【问题标题】:How to get meta data from WooCommerce product attribute terms如何从 WooCommerce 产品属性术语中获取元数据
【发布时间】:2021-06-07 09:44:00
【问题描述】:
特定属性列表的元值包含图像的名称。有没有办法可以访问属性的 meta_values 列表?
这给了我所有的名字,但我也需要 meta_values:
global $product;
$stain=$product->get_attribute('pa_stain-color');
echo $stain;
如何获取元值?我已经尝试了许多变体,但根本无法获得 meta_values。
【问题讨论】:
标签:
php
wordpress
woocommerce
custom-taxonomy
taxonomy-terms
【解决方案1】:
由于每个产品属性都是自定义 WooCommerce 产品分类法,对于特定的自定义分类法,您可以获取附加到产品的术语,然后获取术语元数据,如下所示:
$taxonomy = 'pa_stain-color';
$terms = wp_get_post_terms( get_the_ID(), $taxonomy ); // Get terms for the product
if ( ! empty($terms) ) {
foreach ( $terms as $term ) {
$meta_data = get_term_meta( $term->term_id ); // Get all meta data
// Display the term name
echo '<p>' . $term->name . '</p>';
// Raw array output from term meta data
echo '<pre>' . print_r($meta_data, true) . '</pre>';
}
}
文档:WordPress get_term_meta() function