Support

Account

Home Forums Front-end Issues Displaying an ACF image field for a Tag taxonomy within a loop of posts

Solved

Displaying an ACF image field for a Tag taxonomy within a loop of posts

  • I have created an image field to be used with a tag by settings up the custom field to display in the backend if taxonomy = tag. I can add the image to the tag fine in the backend.
    The tags are set up and associated with individual custom posts (itineraries). I want to be able to display the image within a loop of the custom posts showing on a single.php page for another custom post type.

    The loop code to display the list of itineraries is:

    <?php
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    etc. etc.
    ?>

    Within this loop I am using the following code to display all tags associated with each itinerary in the list

    <?php
    // display tags and links for each post //
    $posttags = get_the_tags();
    if ($posttags) {
    echo”<ul class=’itinerary_tags’>”;
    foreach($posttags as $tag) {
    ?>

    <?php
    }
    ?>

    This give me a list of tags and links fine. HOwever I want to be able to display the image associates with each tag. Could anyone please help with the code needed to do this. Thanks

  • 
    // get the image from the tag
    $image = get_field('image', 'term_'.$tag->term_id);
    
    // output the image
    the_field('image', 'term_'.$tag->term_id);
    
  • Thank you! I edited slightly and now working brilliantly 🙂

    <?php $tag_image = get_field( ‘tag_image’, ‘term_’.$tag->term_id ); ?>
    <?php if ( $tag_image ) { ?>
    ” alt=”<?php echo $tag_image[‘alt’]; ?>” />
    <?php } ?>

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

You must be logged in to reply to this topic.