定位 WooCommerce 类别的高级自定义字段

target a WooCommerce category's advanced custom field

我正在尝试获取 WooCommerce 类别的高级自定义字段。使用以下代码,我得到了 woocommerce 类别:

$categories = get_terms('product_cat');
  var_dump($categories);

但是为什么不包括任何 ACF 信息?是否有其他函数可以获取 ACF 信息?

更新

这是在循环之外。所以我试图获得特定产品类别的自定义字段。我找到了这个信息:http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/

我无法让它工作。

回答

用 get_field() 得到 ACF。我需要使用通过 get_categories() 获得的数组的 cat_ID。 (也许它也适用于 get_terms())

我未能掌握 get_field() 中的第二个参数 我是通过以下方式制作的:

$id = 'product_cat_' . $category->cat_ID;
echo get_field ('field_name', $id);

根据 documentation of ACF 看来您将获得如下自定义术语数据:

$category = get_term_by('slug', 'your-category', 'product_cat');
If( ! is_wp_error( $category ) && $custom_field = get_field('your_custom_field', $category ) ){
   echo $custom_field;
}
// get the current taxonomy term
$term = get_queried_object();

// vars
$image = get_field('image', $term);
$color = get_field('color', $term);

查看文档 here