首页 > 解决方案 > 如何从 WooCommerce 产品属性术语中获取元数据

问题描述

特定属性列表的 meta_value 包含图像的名称。有没有办法可以访问属性的 meta_values 列表?这给了我所有的名字,但我也需要 meta_values:

global $product; 
$stain=$product->get_attribute('pa_stain-color');
echo $stain;

我怎样才能得到 meta_values?我已经尝试了许多变体,但根本无法获得 meta_values。

标签: phpwordpresswoocommercecustom-taxonomytaxonomy-terms

解决方案


由于每个产品属性都是自定义 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>'; 
    }
}

文档:WordPressget_term_meta()功能


推荐阅读