最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

taxonomy - Can't display multiple terms with get_the_terms

programmeradmin2浏览0评论

I am trying to display the terms (from a custom taxonomy) of a single post, but I can't display more than one term. When I try to display all the terms using a foreach loop, it doesn't display anything.

Here is one of my attempts :

<?php 

$terms = get_the_terms($post->ID, 'auteur');

if ($terms && !is_wp_error($terms)) {
    foreach($terms as $term) {
        echo $term->name ;
    }
}
        
 ?>

It doesn't display anything. But the same code without foreach loop displays (as expected) one term :

<?php 

$terms = get_the_terms($post->ID, 'auteur');

   echo $term->name ;
    
?>

I imagine that I'm missing an evident mistake (I'm a beginner), but I can't understand what is wrong with the foreach loop...

Thank you !

I am trying to display the terms (from a custom taxonomy) of a single post, but I can't display more than one term. When I try to display all the terms using a foreach loop, it doesn't display anything.

Here is one of my attempts :

<?php 

$terms = get_the_terms($post->ID, 'auteur');

if ($terms && !is_wp_error($terms)) {
    foreach($terms as $term) {
        echo $term->name ;
    }
}
        
 ?>

It doesn't display anything. But the same code without foreach loop displays (as expected) one term :

<?php 

$terms = get_the_terms($post->ID, 'auteur');

   echo $term->name ;
    
?>

I imagine that I'm missing an evident mistake (I'm a beginner), but I can't understand what is wrong with the foreach loop...

Thank you !

Share Improve this question asked Aug 11, 2020 at 0:38 Paul GPaul G 1 2
  • Well in your second example you’re echoing $term, a different variable that has nothing to do with your use of get_the_terms(). Where are you running this code? Where is $post coming from? – Jacob Peattie Commented Aug 11, 2020 at 0:55
  • Thank you for your reply. I'm running this code in the template single-livre.php (I registered a custom post type "livre" (book) with a custom taxonomy "auteur" (author) ). Then, from the backoffice, I added a new custom post "livre" with two authors in an ACF Taxonomy field. I just want to display the name of the two authors in the post. The first code doesn't display anything, the second one displays one of the two terms "auteur". Do these informations help ? – Paul G Commented Aug 11, 2020 at 12:09
Add a comment  | 

1 Answer 1

Reset to default 0

Your first section of code is correct. This means that if it doesn't output anything, something else is going on and you need to debug your own code to figure out why that is. A good start is to validate that things you think are true are actually true, to look at raw data, and to give yourself feedback on why the if wasn't true. For example, what is $post->ID? what do the raw results look like that come back from get_terms?

Get more information from your own code by doing stuff like this:

$postID = $post->ID;
$taxName = 'auteur';

echo "Getting posts in ta $taxName for post ID $postID"; // Output what the variables are before you use them

$terms = get_the_terms($postID, $taxName);

print_r($terms); // Look at the raw data you get back in case your foreach or output has an error in it

if (empty($terms)) { // Was it empty?
   echo "terms empty or unset";
} elseif (is_wp_error($terms)) { // was there an error? If so print it out?
   echo "there was an error";
} else {
    foreach($terms as $term) {
        echo $term->name ;
    }
}
发布评论

评论列表(0)

  1. 暂无评论