Display image after taxonomy text with advanced custom fields

I have created a custom post type called:’activities’ and created a taxonomy for it called: ‘activity_locations’. I have then added a custom field to the taxonomy for an image using Advanced Custom Fields, this image field is called: ‘activity_location_image’

On the single template I have managed to display the taxonomy for the product with the following code:

Read More
Available in: <?php the_terms( $post->ID, 'activity_locations', ' ', ' / ' ); ?>

However I need to elaborate on this to add a small image after the taxonomy text. I have tried the following code but it hasn’t worked (nothing is displaying):

<?php
$term = get_field('test');
  if( $term ): ?>
  <img src="<?php echo get_field('activity_location_image', $term); ?>" />
<?php endif; ?>

Can anyone offer any advice/assistance on how to make this work?

Related posts

Leave a Reply

1 comment

  1. If you are still on the single template page, then get_field() without a second argument will only be fetching metadata for your current post object, not the related term objects.

    If you read the documentation for get_field() you’ll see that to query for term fields you need to use the format

    get_field('field_name', 'taxonomyname_X)

    Where taxonomyname would be activity_locations and X would be the term ID.

    Since you want to customize the display and use the data about the terms, you’ll have to swap the_terms() with wp_get_post_terms() which will give you an array of term objects. You can then loop over the array, grabbing the image for each term and outputting the HTML to display the term.