Home Forums General Issues How to display a Taxonomy content instead of ID?


How to display a Taxonomy content instead of ID?

  • Hello! ๐Ÿ™‚

    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. 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:

    $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.

    Any hints?

  • 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 category_insect_family or insect_family? Make sure you use the correct field key.

  • It worked as you suggested Mark ๐Ÿ™‚

    Thanks so much for your help!

  • Hello Mark,

    Any chance you can look at my other post

    And /or a support ticket I sent for the same issue.

    Thank you!

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

The topic ‘How to display a Taxonomy content instead of ID?’ is closed to new replies.