【问题标题】:Get name of the taxonomy name from the term idWordpress - 从术语 id (ACF) 中获取分类名称的名称
【发布时间】:2017-09-20 11:08:46
【问题描述】:

我一直讨厌分类学,它太混乱了。我正在使用 ACF。我有一个自定义用户注册字段调用国家、出口、公司(它们都是分类法)。

例如。 taxonomy=country&tag_ID=36&post_type=company

$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。但它返回给我 arr

【问题讨论】:

    标签: wordpress categories advanced-custom-fields taxonomy term


    【解决方案1】:

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

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

    //

    认为您的 get_terms 语法错误。

    【讨论】:

    • $countrydata = get_terms('country',array('include' => array($country_tagid))); print_r($countrydata);它返回数组 ( )
    【解决方案2】:

    我发现这条线是错误的。它没有将值 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
    

    我通过

    检索id
    $countryid= $country_tagid[0];
    

    然后通过以下方式检索国家/地区名称:

    $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      相关资源
      最近更新 更多