1

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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/

1 Comment

Thank you, this works. I was trying all sorts of convoluted solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.