WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. dorich
    Member
    Posted 1 year 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
    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

  2. vtxyzzy
    Member
    Posted 1 year 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