Home › Forums › General Issues › Taxonomy Term values from user profile
Hello
I have a taxonomy called “subject” that is part of a CTP called “topics”.
I have a custom field group with a location rule set for all users.
I have a custom field called “specialist_topic” that is set to the Taxonomy Field Type. Within this I select “subject” from the Taxonomy drop-down, “Select” from the Field Type drop-down, and Term Object from the Return Value field.
When editing a User Profile, I select a taxonomy term from the custom field and save it.
In my author.php file, I’d like to display the taxonomy term value. I am doing this like so:
// Get author ID
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
// Var for custom field
$specialtopic = get_field('specialist_topic', 'user_' . $curauth->ID);
// Output the taxonomy term
echo $specialtopic;
I get this error:
Catchable Fatal Error: Object Of Class StdClass Could Not Be Converted To String In
Var Dump shows (“Food” is the correct tax term)
Object(StdClass)#257 (9) { ["Term_id"]=> String(3) "122" ["Name"]=> String(4) "Food" ["Slug"]=> String(4) "Food" ["Term_group"]=> String(1) "0" ["Term_taxonomy_id"]=> String(3) "128" ["Taxonomy"]=> String(7) "Subject" ["Description"]=> String(20) "Top 10 Food Websites" ["Parent"]=> String(1) "0" ["Count"]=> String(1) "3" }
Alternatively, when I change the Return Value of ID I can happily output the ID # without the error.
Any ideas where I’m going wrong?
Thanks
Dave
Hi @juxprose
Because you have selected ‘Term Object’ as the return type, you are getting an object (just like you have chosen).
This object can be seen clearly in your var_dump.
Is there a problem that I am not seeing? Do you need help rending the object? This would work:
<?php
$specialtopic = get_field('specialist_topic', 'user_' . $curauth->ID);
?>
<h3><?php echo $specialtopic->name; ?></h3>
Thanks Elliot, my echo wasn’t correct, your ‘<?php echo $specialtopic->name; ?>’ fixes the problem just as I’d like.
Many thanks for your time looking at this, and the fix.
Thanks
Dave
Hi @juxprose
Thanks mate. Can you change this topic to a question and mark the appropriate reply as the solution?
The topic ‘Taxonomy Term values from user profile’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.