[resolved] How To Extract A Single Value from wp_get_post_terms (2 posts)

  1. dorich
    Posted 3 years ago #

    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
    But that doesn't return anything either.

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


  2. vtxyzzy
    Posted 3 years ago #

    $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;

Topic Closed

This topic has been closed to new replies.

About this Topic