我正在尝试获取WooCommerce类别的高级自定义字段。使用以下代码,我获得了wooccommerce类别:
$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);
根据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);
请参阅此处的文档