Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How can I echo multiple term values types using wp_get_post_terms WordPress



This is driving me nuts. Help please.

I am simply trying to to echo different parts of my terms, name, slug etc... All within my loop.

I have this...

$group = wp_get_post_terms( get_the_ID(), 'group', array("fields" => "all"));

If I print_r($group);

it returns this...

<strong>Array ( [0] => stdClass Object ( [term_id] => 6 [name] => Street [slug] => street [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => group [description] => [parent] => 0 [count] => 8 ) )</strong>

So if I remove the print_r and start breaking down the array into bits doing this inside my loop.

<?php echo $group['name']; // to return my term Name ?>

<?php echo $group['slug']; // to return my term Slug ?>

I get nothing!!! Why does nothing echo?


Answers (1)


Arnav Joy answers:

try this

foreach ( $groups as $gp ) {

echo 'Name==>'. $gp->name;
echo '<br>slug==>'. $gp->slug;

or in your case try this

echo $groups[0]->name;
echo $groups[0]->slug;


Arnav Joy comments:

you are not getting any values because you are trying to access a array index but the return variable is an object

so you have to use


Arnav Joy comments:

please change the name of the variable to


i have mistakenly placed an extra 's' to it beome $groups

Josh Cranwell comments:


Thanks Arnav!