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 !

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? 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
    Aug 11, 2020 at 12:09

1 Answer 1

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 ;
    }
}
1
  • Thank you for your help. Your code displays: "Getting posts in ta auteur for post ID 80terms empty or unset". So I imagine you're right, the issue doesn't comme from where I thought. You asked me about $post->ID, and in fact I don't know... I tried different parameters like $post, $post->ID, get_the_ID() ... I thought it was ok because I can display one of the two terms "auteur" with the second code, but now I realize that terms are not found. I described in a previous comment my whole process (register taxonomy "auteur", add two terms to a single custom post with an ACF tax field,etc.)
    – Paul G
    Aug 11, 2020 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.