Support

Account

Home Forums General Issues Display value for taxonomy in template

Solved

Display value for taxonomy in template

  • Hello, I am new to ACF & WordPress as I am in the process of migrating from drupal to wordpress (well giving it a go anyway). As I am new to wordpress please excuse & correct me if I am looking at this problem incorrectly.

    This is a two-parter:

    I have a custom post type, within that post type I have a number of custom taxonomies with a few terms in each.

    My custom post type has, for this example, three custom fields:

    • One field references one custom taxonomy.
    • Two more fields BOTH reference another custom taxonomy.

    To put it as an example:
    Post Type = Holiday
    Taxonomy 1 = Location
    Taxonomy 2 = Equipment

    The user can select one term from the Location taxonomy in a custom field. There are two further custom fields one called “Required Equipment” and the other “Optional Equipment”. I want those fields to both link to the equipment taxonomy.

    This is essentially my set-up. Am I approaching this incorrectly?

    Part 2:

    This is probably a simple one but I have searched, and searched… and searched.

    How do I print/render the values from these custom fields in my template as terms and not IDs?

    I have tried the_field and get_field and they work but output the terms as an ID. I want them to output the relevant name of that term.

    Thanks, I understand the answer is probably simple but any help would be greatly appreciated.

  • Hi @kylewallace86

    #2
    When you edit the taxonomy field, you can set the return type setting from ID to Object. The Object will return all the term data from the DB, not just the ID! Please note, you need to use the get_field function to load the value.

    #1
    Create a field group for the term fields (“Required Equipment” and the other “Optional Equipment”) and use the location rules to map this field group to the taxonomy term under ‘Other’.

    Now, when you edit the term, you can save this extra data.

    Now, back to your template. You should be on a single.php or something similar to display the $post. You can load the term like so:

    
    $term = get_field('equipment');
    

    Then you can load the equiptment data like so:

    
    $required_equipment = get_field('required_equipment', $term);
    

    Notice that you can pass the $term object through to the function to tell ACF to load from that object, not from the global $post object.

    Hope that helps.

    Please note that to pass in a $term object, you will need to be using a recent version of ACF (4.3.3)

    Thanks
    E

  • Thanks Elliot for your response,

    I gave it another go but I am just not getting it, I have only basic PHP skills and can’t seem to wrap my head around such a simple problem.

    I now get the following error:
    Catchable fatal error: Object of class stdClass could not be converted to string in DIRECTORY/FILE

    A dump gives me this:
    object(stdClass)#436 (10) { ["term_id"]=> int(9) ["name"]=> string(8) "Test Term" ["slug"]=> string(8) "test_term" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(9) ["taxonomy"]=> string(18) "test_taxonomy" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" }

    Note I am using “Test Taxonomy” and “Test Term” while I practice.

    Are you aware of any simple guide/tutorial on the net that might assist me with some simple setup steps? I don’t want to waste anybodies time on here if I can help it.

    I appreciate your assistance so far.

    Edit: WP 3.8 & ACF 4.3.4

  • Disregard my last. Like I said I lack PHP skills but will remember this for next time. I got my answer to the last from http://support.advancedcustomfields.com/forums/topic/taxonomy-term-values-from-user-profile/. I will mark as solved if I work out the explanation you gave for #1. Thanks!

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

The topic ‘Display value for taxonomy in template’ is closed to new replies.