WordPress的 - 从术语id(ACF)获取分类名称的名称

问题描述:

我总是讨厌分类,它是如此混乱。我正在使用ACF。我有一个自定义用户注册字段调用国家,出口,公司(他们都是分类学)。WordPress的 - 从术语id(ACF)获取分类名称的名称

例如,分类=国家& TAG_ID = 36 & post_type =公司

$country_tagid = the_field('country', 'user_'.$user_id->ID); //This output 36 
$countrydata = get_terms('country',array('id'=>$country_tagid)); // 
echo $countrydata->name; //return nothing 
$getcountrydata = get_term($country_tagid); 
print ($getcountrydata->name); //return nothing 

他们都没有返回的名称。我预计它会返回'泰国'。哪里不对?

编辑: 奇怪的是,我手动进入我的用户循环外。

<?php 
    $catinfo = get_term_by('id', 36, 'country'); 
    print $catinfo->slug; //thailand 
?> 

这有效。 我怀疑什么是错在这里

$country_tagid = the_field('country', 'user_'.$user_id->ID); //This output 36 

此行打印36现在我想get_field。但它返回我的常用3

试试这个: https://developer.wordpress.org/reference/functions/get_terms/

$countrydata = get_terms('country',array('include' => array($country_tagid)); 

//

认为你对get_terms语法是错误的。

+0

$ countrydata = get_terms(” country',array('include'=> array($ country_tagid))); print_r($ countrydata); 它返回Array() –

我发现这条线是错误的。它没有将值36存储在变量中。而是显示它。

$country_tagid = the_field('country', 'user_'.$user_id->ID); //This output 36 

我改为get_field来代替。

$country_tagid = get_field('country', 'user_'.$user_id->ID); //This output 36 

我检索由

$countryid= $country_tagid[0]; 

的ID,然后在检索由国家名称:

$catinfo = get_term_by('id', $countryid, 'country'); 
$countryname= $catinfo->name; 

所以全码:

$country_tagid = get_field('country', 'user_'.$user_id->ID); 
$countryid= $country_tagid[0]; 
$catinfo = get_term_by('id', $countryid, 'country'); 
$countryname= $catinfo->name;