Reputation: 160
I have created a custom taxonomy for my custom post type.
Each term only needs to display its name (which works with $term->name;
) and an image. For the image I am using ACF (as I usually do for custom fields on a page or post). It is set up and I can select an image when I add a new term in the dashboard, but how do I display it in a foreach loop? The usual get_field()
isn't showing anything.
<ul class="taxonomy-terms">
<?php
$terms = get_terms( array(
'taxonomy' => 'my_taxonomy',
'hide_empty' => false,
) );
?>
<?php foreach ( $terms as $term) { ?>
<li>
<img src="<?php the_field('image'); ?>" />
<?php echo $term->name; ?>
</li>
<?php } ?>
</ul>
The issue being with the_field('image');
The image field is set to return the image url.
Upvotes: 1
Views: 2146
Reputation: 420
You should pass the current term of the loop as the second parameter of the ACF function:
<?php the_field('image', $term); ?>
See here: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Upvotes: 2
Reputation: 80
Maybe you should add your taxonomy as a parameter when calling the image, try :
<?php if( get_field('image') ): ?>
<img src="<?php the_field('image', $term); ?>" />
<?php endif; ?>
It's better to test the Image Field as you don't wan't to show an empty <img>
tag.
Also please check that you didn't change the field name in the Image Field settings.
You can read about that here : https://www.advancedcustomfields.com/resources/image/ And here : https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Let's hope this works,
Have a good day.
Upvotes: 2