• Resolved dorich

    (@dorich)


    I want to display the term attached to a post in certain taxonomy.
    I can get the taxonomy information for a post using
    $sectiondata = wp_get_post_terms($post->ID, 'chapters', array("fields" => "all"));
    Then using
    print_r $sectiondata;
    I can display the array of values returned.

    However, how do I echo the value for the ‘name’ of the term to the page?
    I thought this should be something like:
    echo $sectiondata->name;
    But that returns nothing so I obviously don’t understand how to extract this value. I’ve searched around for examples and don’t see anything that explains how to display the value on the page, or perhaps better stated how to extract the value from the array. I’ve tried using the plain php approach of
    print($sectiondata['name']);
    But that doesn’t return anything either.

    Where can I find an explanation of how to extract the value from the array.

    Thanks

Viewing 1 replies (of 1 total)
  • $sectiondata is an array of objects. You must select one of the array entries before you can reference its properties (fields).

    This code will print the ‘name’ of the first object (index zero):

    if ($sectiondata) {
       echo $sectiondata[0]->name;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How To Extract A Single Value from wp_get_post_terms’ is closed to new replies.