Correct use of get_the_terms()

I need to print all terms associated to a custom post type post.
In the post template I wrote that code:

<?php foreach (get_the_terms(the_ID(), 'taxonomy') as $cat) : ?>
     <?php echo $cat->name; ?>
<?php endforeach; ?>

The loop works correctly, but before the list also the id was printed. Like:

37
taxonomy01
taxonomy02
taxonomy03

What is wrong?

2 Answers
2

the_ID() print the post ID. You need to use the get_the_ID() which return the post ID.

Example:

foreach (get_the_terms(get_the_ID(), 'taxonomy') as $cat) {
   echo $cat->name;
}

Always remember the naming convention of WordPress for template tags. the which mean to print get which mean to return in most of the cases.

Leave a Comment