如何访问分类中的acf?
How to access the acf in taxonomy?
我有一个名为 country 的分类法。如图所示,我使用 acf 添加了 2 个新的自定义字段
但是当我尝试访问这些字段时,我找不到它们。我只有
"country": {
"term_id": 1,
"name": "aaaa",
"slug": "aaaa",
"term_group": 0,
"term_taxonomy_id": 1,
"taxonomy": "country",
"description": "",
"parent": 0,
"count": 0,
"filter": "raw"
},
我试过这样做,但我没有得到值。
$terms = get_terms( array(
'taxonomy' => 'country',
'hide_empty' => false, ) );
$output = '';
foreach($terms as $term){
//$output .= $term->name . '-';
$output .= get_field( 'flag', 'country'.'_'.$term->term_id );
}
我做错了什么?
谢谢
要获得 acf 字段,您应该使用 get_field()
要获取分类字段,请使用此语法
$variable = get_field('fields_slug', $term);
在你的情况下,如果你是
$country_flag = get_field('flag', $term);
$country_currency = get_field('currency', $term);
P.S。使用您在创建这些字段时设置的 slug 更改标志和货币,$term 是一个纯分类对象,而不是 id 或 slug。
我有一个名为 country 的分类法。如图所示,我使用 acf 添加了 2 个新的自定义字段
但是当我尝试访问这些字段时,我找不到它们。我只有
"country": {
"term_id": 1,
"name": "aaaa",
"slug": "aaaa",
"term_group": 0,
"term_taxonomy_id": 1,
"taxonomy": "country",
"description": "",
"parent": 0,
"count": 0,
"filter": "raw"
},
我试过这样做,但我没有得到值。
$terms = get_terms( array(
'taxonomy' => 'country',
'hide_empty' => false, ) );
$output = '';
foreach($terms as $term){
//$output .= $term->name . '-';
$output .= get_field( 'flag', 'country'.'_'.$term->term_id );
}
我做错了什么? 谢谢
要获得 acf 字段,您应该使用 get_field() 要获取分类字段,请使用此语法
$variable = get_field('fields_slug', $term);
在你的情况下,如果你是
$country_flag = get_field('flag', $term);
$country_currency = get_field('currency', $term);
P.S。使用您在创建这些字段时设置的 slug 更改标志和货币,$term 是一个纯分类对象,而不是 id 或 slug。