代码之家  ›  专栏  ›  技术社区  ›  Matt - Salusa Community

如何仅在get_术语中呼出第一个术语

  •  0
  • Matt - Salusa Community  · 技术社区  · 7 年前

    不知您是否可以帮助修复我下面的代码。我试图重复作者第一篇文章中的第一类分类法。

    我想我就快到了。我可以让它输出所有类别标题,但我的代码中的问题是,我试图只输出第一个类别。

    <?php
    
    $args = array(
        'author' => intval(bp_displayed_user_id()),
        'post_type' => 'job_listing'
    );
    
    $listings_of_author = get_posts($args);
    $postid = $listings_of_author[0]->ID;
    $term_list = wp_get_post_terms($postid, 'job_listing_category', true);
    $firstTerm = $terms_list[0];
    
    if (get_the_terms($author, 'job_listing_category', true)) {
        echo $fistTerm->name;
    }
    
    ?>
    

    非常感谢你的帮助。

    马特

    1 回复  |  直到 7 年前
        1
  •  0
  •   larsAnders    7 年前

    有几种拼写错误可能会妨碍其正常工作:

    $term_list = wp_get_post_terms($postid, 'job_listing_category', true);
    $firstTerm = $term*s*_list[0]; //should be term_list
    

    ...后来

    echo $fistTerm->name; //should be firstTerm