1

I am using WordPress' native function get_the_terms and it returns this array with an object inside:

array (size=2)
  157 => 
    object(stdClass)[41]
      public 'term_id' => int 157
      public 'name' => string 'Entertainment' (length=13)
      public 'slug' => string 'entertainment' (length=13)
      public 'term_group' => int 0
      public 'term_taxonomy_id' => int 157
      public 'taxonomy' => string 'category' (length=8)
      public 'description' => string '' (length=0)
      public 'parent' => int 0
      public 'count' => int 1
      public 'object_id' => int 644
      public 'filter' => string 'raw' (length=3)
  151 => 
    object(stdClass)[40]
      public 'term_id' => int 151
      public 'name' => string 'Featured' (length=8)
      public 'slug' => string 'featured' (length=8)
      public 'term_group' => int 0
      public 'term_taxonomy_id' => int 151
      public 'taxonomy' => string 'category' (length=8)
      public 'description' => string '' (length=0)
      public 'parent' => int 0
      public 'count' => int 1
      public 'object_id' => int 644
      public 'filter' => string 'raw' (length=3)

How do I access

  public 'name' => string 'Featured' (length=8)

This works

$terms = get_the_terms( $post->ID, 'category' );
foreach ($terms as $term) {
    $test = $term->name;
    echo $test;
}

This does not work:

for($i=1; $i<3; $i++) {
    $term = $terms->name;
    echo $term;
}

nor does this work

for($i=1; $i<3; $i++) {
    $term = $terms[0]->name;
    //$term = $terms[1]->name;
    //$term = $terms[157]->name; // works but not reliable
    echo $term;
}

Why?

3
  • 2
    Because you aren't using the index to access the aray member you want. Try $term = $terms[$i]->name; Commented Aug 23, 2014 at 20:23
  • why this must works^)? Commented Aug 23, 2014 at 20:23
  • I did that and using terms[157] works but isnt that subject to change? I just want the first spot in the array. terms[0]->name does not work. Commented Aug 23, 2014 at 20:25

3 Answers 3

1

The function get_the_terms is returning an array, which is indexed by the term_id of each element.

That is why $terms[157] and $terms[151] are working as you expect. See the PHP reference on arrays for details of this behavior.

Your best bet is to stick with the built in foreach which works as stated in your question.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @dehrg but see my comment above. This is one of the first things I've tried in the for loop.
Yes the point of the for loop is to limit the amount of categories displayed. In this case only 2.
Why don't you want to use the foreach? Given that the function is returning an array with odd indices, that seems like your best bet.
Ok, and just build in the incrementing var to limit the loop? But I'm more concerned with why terms[0] or terms[1] won't work.
Edited to answer your question
1

In the for loop you are trying to access name directly within the array, not the stdclass objects.

You need to use the $i variable as the index of the array.

for ($i=1; $i<3; $i++) {
    $term = $terms[$i]->name;
    echo $term;
}

Comments

0

Function using the wp_parse_args system to manage its single $args argument, which could be given whatever values you wanted. In this case $args stores detailed display overrides, a pattern found in many WordPress functions.

$args = wp_parse_args( $args, $term->name );
echo $arg[157]['term_id']; //output 157
echo $arg[157]['name'];  //output Entertainment

works fine for me more detail

http://codex.wordpress.org/Function_Reference/wp_parse_args

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.