Support

Account

Home Forums Front-end Issues Displaying Multiple Values from Custom Taxonomy Terms

Solved

Displaying Multiple Values from Custom Taxonomy Terms

  • I have a custom taxonomy (Industry) which has several terms (manufacturing, hospitality, etc.). I created a new ACF field group with a rule to apply to the industries taxonomy. My settings are:
    Required = No
    Taxonomy = Industry
    Field type = Checkbox
    Allow null = No
    Load & save terms to post = No
    Return Value = Term ID

    On my taxonomy term template (taxonomy-industry.php), I want to display the list of “related industries.” My code is:

    $term = get_queried_object();
    if ( $term ):
    ?>
    <p><?php echo get_field( ‘related_industries’, $term->taxonomy . ‘_’ . $term->term_id );

    All that returns is: Array

    I’m successfully getting a custom image from the same taxonomy term, but unable to display the related taxonomy term names. I’m pretty sure I need to somehow display the array instead of a single field.

    Any help is appreciated.

  • UPDATE: Same settings as above but updated code. Now getting proper list of custom field id’s on my related term, but can’t seem to display the name instead of the ID.

    
    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;
    
    // load thumbnail and related industries for this taxonomy term
    $thumbnail = get_field('industry_image', $queried_object);
    $related = get_field('related_industries', $queried_object);
    print '<img src="';
    print $thumbnail;
    print '" class="industry-page-img" />';
         echo '<ul>';
         foreach ( $related as $term ) {
           echo '<li>' . $term . '</li>';
         }
         echo '</ul>';

    The above returns the image as expected, but only displays the proper term ID. I assumed printing the $term->name would display the field name, but it doesn’t.

  • This line

    $related = get_field('related_industries', $queried_object);

    Is returning an array of term ID values. If you want to show information about that the terms then you should set it to return Term Object then loop over the returned array to display what you want.

    
    if ($related) {
      foreach ($related as $term) {
        echo $term->name;
      }
    }
    
  • Thanks John… worked like a charm. I feel like an idiot, but learning! Much appreciated.

  • No reason to feel like an idiot, everyone needs to start somewhere. Glad to help.

  • Okay… so another question if you don’t mind. I’m additionally hoping to display an image assigned to this taxonomy term. The image field was also created by ACF (industry_image) in a separate field group and attached to my taxonomy term “industries.”

    // grab the "related industries" terms from the current queried object
    $related = get_field('related_industries', $queried_object);
    
    // loop through the returned terms and display the name and image
    if ($related) {
      foreach ($related as $term) {
        echo $term->name;
        // what I want to display in addition
        echo $term->[ACF field assigned to this term, 'industry_image']
      }
    }

    My $related variable above is only including the related_industries field and not the other other custom field, industry_image. Perhaps I need a special query to grab all the custom fields for the current taxonomy term.

  • The exact code to use will depend on what you’re returning from the image field.

    Generally what you need to do is supply the right post_id value for the ACF field. This example will get the value in the image field.

    
    $image = get_field('industry_image', $term->taxonomy.'_'.$term->term_id);
    

    The image field documentation goes into more detail about displaying the image based on what you’re returning which can be an Object, URL or ID. http://www.advancedcustomfields.com/resources/image/

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

The topic ‘Displaying Multiple Values from Custom Taxonomy Terms’ is closed to new replies.