The Advocates
The Advocates

Reputation: 21

How do I echo out a value from WP_term object?

So All I want to do is to echo the 'name' value on an <h1> basically. So I am wanting to display the taxonomy name this current Post belongs to and I am able to get it by doing this:

   $term_obj_list = get_the_terms( $post->ID, 'state');

So this particular post belongs to the Nebraska state so if I do a var dump on $term_obj_list i get this:

    var_dump($term_obj_list);

This is that it prints:

    array (size=1)
  0 => 
    object(WP_Term)[3262]
      public 'term_id' => int 116
      public 'name' => string 'Nebraska' (length=8)
      public 'slug' => string 'nebraska' (length=8)
      public 'term_group' => int 0
      public 'term_taxonomy_id' => int 116
      public 'taxonomy' => string 'state' (length=5)
      public 'description' => string '' (length=0)
      public 'parent' => int 0
      public 'count' => int 9
      public 'filter' => string 'raw' (length=3)

So there it is public 'name' => string 'Nebraska' (length=8) How am I supposed to echo this to an html line?

I tried echo $term_obj_list-> name but it didn't work. What am I doing wrong?

Upvotes: 1

Views: 3384

Answers (1)

Everett
Everett

Reputation: 9628

Because it appears to return an array of objects, you would first need to specify the index in the array before attempting to read the object attributes.

Try:

echo $term_obj_list[0]-> name

Upvotes: 2

Related Questions