Home › Forums › Front-end Issues › 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!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.