Home › Forums › General Issues › How to display a Taxonomy content instead of ID?
I am new to ACF and trying to create my first website using it.
I am trying to learn how to do something and I can’t figure it out. I was hoping someone can help me understand how can I do it (I hope it’s an easy one as I am a beginner coding).
In ACF if have a field that pulls categories from a CPT with a select field.
When I try to display this field in my page, it’s displaying the category ID instead of the content of that category (the category name). I imagine that this happens because this field in ACF is only saving the category ID and not the content of the category itself.
How can I pull the category content instead of the ID?
My fields are:
CPT = Insects (all_insects)
Custom Field = Family (insect_family)
Taxonomy = Insect Families (category_insect_family)
Thank you very much!
You can configure the return value of the field to give you the Taxonomy (category) object instead of the ID.
Thank you Mark.
Unfortunately I am a beginner coding and I am not sure what you mean exactly. Could you please expand a little bit? Is there any documentation you can point me to, or code example I can try to analyse to see if I can reproduce what you are suggesting?
Thank you for trying to help me 🙂
@nano09 Take a look at the “Return Value” field in the Taxonomy field configuration. https://www.advancedcustomfields.com/resources/taxonomy/ It allows you to choose whether you want the Term ID or the Term Object in your code.
Oh yes, I see what you mean now. Yes, it is still not working. It is not retrieving the value regardless of what option I choose there. It shows blank.
Maybe it is because only the ID for the category is stored in the post and the value in the category?
I’ll try again and will let you know. Thank you! 🙂
Yes, unfortunately nothing is showing. I’m using a page builder and inserting a code block where I input:
<?php $term = get_field('category_insect_family'); if( $term ): ?> <h2><?php echo $term->name; ?></h2> <p><?php echo $term->description; ?></p> <?php endif; ?>
Result is blank with any option (ID or Object) for Custom Field = Family (insect_family).
If I change:
$term = the_field('insect_family');
I get the category ID value – if field is setup to return ID. When I change it to object this goes blank again.
When you change the return type, you may need to save the Post/Page again. Have you tried passing the Post/Page ID to the
get_field function as the second parameter?
Also: your two examples aren’t referencing the same field category_insect_family. Is the field called
insect_family? Make sure you use the correct field key.
It worked as you suggested Mark 🙂
Thanks so much for your help!
Any chance you can look at my other post https://support.advancedcustomfields.com/forums/topic/gallery-field-query-filtering-by-taxonomies/
And /or a support ticket I sent for the same issue.
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.