Support

Account

Home Forums Front-end Issues Display Taxonomy field from within Group

Solved

Display Taxonomy field from within Group

  • Hi there,

    I’m using the ACF fields Group (“team”), and within that are various fields for taxonomies selection (“director”, “artist” etc.).

    My front-end output should be something like:

    Director: term1, term2, term3
    Artist: term1, term5, term33

    Currently I’m using the taxonomy to return an object, then using print_r to display the entire Director array:

    print_r($team['Director');

    Output

    Array
    (
        [0] => WP_Term Object
            (
                [term_id] => 157
                [name] => John Doe
                [slug] => john-doe
                [term_group] => 0
                [term_taxonomy_id] => 157
                [taxonomy] => team
                [description] => 
                [parent] => 0
                [count] => 2
                [filter] => raw
            )
    
        [1] => WP_Term Object
            (
                [term_id] => 101
                [name] => Franz Hansen
                [slug] => franz-hansen
                [term_group] => 0
                [term_taxonomy_id] => 101
                [taxonomy] => team
                [description] => 
                [parent] => 0
                [count] => 2
                [filter] => raw
            )
    
        [2] => WP_Term Object
            (
                [term_id] => 105
                [name] => Jan Jansen
                [slug] => janjansen
                [term_group] => 0
                [term_taxonomy_id] => 105
                [taxonomy] => team
                [description] => 
                [parent] => 0
                [count] => 2
                [filter] => raw
            )
    )

    How do I turn this into:

    Director: John Doe, Franz Hansen, Jan Jansen

    Thanks in advance!

  • There are many ways to accomplish this, here’s one that will also put a comma after each name, except the final name.

    
    // Loop through all directors
    foreach ($team['Director'] as $director) {
    
        // Build an array of just the director names
        $names[] = $director["name"];
    
    }
    
    // Display a comma separated list of the names 
    echo implode(", ", $names);
    

    Hope that helps!

  • Hi @speakincode, thank you for your help!

    I’ve been bashing my head at it for hours yesterday. Your code throws a fatal error:

    Fatal error: Uncaught Error: Cannot use object of type WP_Term as array in ...

    What eventually seemed to work was this. Don’t know if it’s very elegant PHP…

    $directorArray = $team['Director'];
    $commaArray = $director->name;
    $commaArray = array();
    foreach ($directorArray as $director){ 
        $commaArray[] = '<span>'.$director->name.'</span>';
    }
    echo implode( ', ', $commaArray );
  • Whoops! Sorry, I mistook each $director in $team['Directors'] to be an array when each is actually an object. So our solutions are actually the same, except yours is using the proper syntax for a property on an object $director->name, where I was using the syntax for a property on an array $director['name']. I hope my mistake didn’t cost ya too much time, sorry about that.

    I hope that clarifies though why one works now and the other threw an error. Also, your code looks just great, but you don’t need the $commaArray = $director->name; line.

    I’m glad you got it worked out!

  • No worries, my bashing my head was before your solution popped up 🙂

    I’m not too familiar with PHP and arrays/objects, so the solution was indeed to realise if I was working with arrays or objects, or objects inside arrays.

    Thanks for helping out!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Display Taxonomy field from within Group’ is closed to new replies.