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:
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?
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 formatget_field('field_name', 'taxonomyname_X)
Where
taxonomyname
would beactivity_locations
andX
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()
withwp_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.