【发布时间】:2016-05-09 20:57:54
【问题描述】:
我需要从 woocommerce 产品变体中获取属性。
$terms = get_post_meta($value['variation_id'], 'attribute_pa_color', true);
这段代码给了我一个属性 slug 而不是名称。如何获取属性名称?
非常感谢您!
【问题讨论】:
标签: php variables attributes woocommerce slug
我需要从 woocommerce 产品变体中获取属性。
$terms = get_post_meta($value['variation_id'], 'attribute_pa_color', true);
这段代码给了我一个属性 slug 而不是名称。如何获取属性名称?
非常感谢您!
【问题讨论】:
标签: php variables attributes woocommerce slug
你可以试试下面的代码。
$terms = get_the_terms( $value['variation_id'] , 'attribute_pa_color');
foreach ( $terms as $term ) {
echo $term->name;
}
如果有帮助,请告诉我。此外,您可以通过this 链接中给出的说明了解更多信息和替代解决方案。
【讨论】:
你得到的是分类学的蛞蝓......
在 WooCommerce 中,没有 attribute_ 的 attribute_pa_color 是一个分类。
所以你可以尝试这样的事情.. 通过 slug 获取术语。并得到它的名字。
$taxonomy = 'pa_color';
$meta = get_post_meta($value['variation_id'], 'attribute_'.$taxonomy, true);
$term = get_term_by('slug', $meta, $taxonomy);
echo $term->name;
【讨论】:
您可以通过以下代码轻松找到属性
foreach ($product->attributes as $taxonomy => $attribute) {
foreach ($attribute->get_terms() as $term) {
var_dump($term); // term object data
}
}
【讨论】: