问题 如何在变体中获取属性名称而不是slug?


我需要从woocommerce产品变异获得属性。

$terms = get_post_meta($value['variation_id'], 'attribute_pa_color', true);

这段代码给了我一个属性slug而不是name。我怎样才能获得属性名称?

非常感谢你提前!


3407
2018-02-01 12:00


起源



答案:


你得到的是分类法的slu .. 在WooCommerce中, attribute_pa_color 没有 attribute_ 是一种分类法。

所以你可以试试这样的东西......用slug来获得这个词。得到它的名字。

$taxonomy = 'pa_color';
$meta = get_post_meta($value['variation_id'], 'attribute_'.$taxonomy, true);
$term = get_term_by('slug', $meta, $taxonomy);
echo $term->name;

12
2018-02-01 14:43



那很有用,呜呜!非常感谢! - Pupik


答案:


你得到的是分类法的slu .. 在WooCommerce中, attribute_pa_color 没有 attribute_ 是一种分类法。

所以你可以试试这样的东西......用slug来获得这个词。得到它的名字。

$taxonomy = 'pa_color';
$meta = get_post_meta($value['variation_id'], 'attribute_'.$taxonomy, true);
$term = get_term_by('slug', $meta, $taxonomy);
echo $term->name;

12
2018-02-01 14:43



那很有用,呜呜!非常感谢! - Pupik


您可以尝试以下代码。

$terms = get_the_terms( $value['variation_id'] , 'attribute_pa_color');

foreach ( $terms as $term ) {
    echo $term->name;
}

如果有帮助,请告诉我。另外你可以通过给出的解释 这个 链接了解更多信息和替代解决方案。


-1
2018-02-01 14:17



不工作,但我会尝试做某事。谢谢你的提示! - Pupik
它适用于:get_the_terms($ product-> id,'pa_color');但它返回所有变化的属性:( - Pupik
尝试使用硬编码变体ID而不是产品ID,但它无法正常工作。我的猜测是不应该以这种方式使用(带有变体ID)? - Pupik