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 } ?>

  • Hey John, hopefully you can help me. I am working with ACF Theme Code Pro and the provided code I think works only on an archive page (?). I am just trying to output an image field put on my taxonomy in a WP_Query. Is the below code should be working considering that I need to replace “NULL” with something else? Thanks a lot!

    <?php 
    					
    // Acf Taxonomy Fields
    $taxonomy_prefix = 'category';
    $term_id = NULL;
    $term_id_prefixed = $taxonomy_prefix .'_'. $term_id;
    $image = get_field( 'logo_image', $term_id_prefixed );
    
    // args
    $args = array(
      'order'       => 'DESC',
      'orderby'     => 'date',
      'posts_per_page'	=> -1,
      'post_type'		=> 'post',
    );
    
    // query
    $posts_query = new WP_Query( $args );
    
    ?>
    <?php if( $posts_query->have_posts() ): ?>
      <?php while ( $posts_query->have_posts() ) : $posts_query->the_post(); ?>
      
      <div class="swiper-slide">
        <a href="<?php the_permalink(); ?>" class="c-card-article">
          <div class="c-card-article__img mb-15">
            <div class="c-card-article__overlay">
              <i class="icon-arrow-right"></i>
              <div><?php _e('Plus de détails', 'credo'); ?></div>
            </div>
    
            <?php if ( $image ) : ?>
              <img src="<?php echo esc_url( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" class="c-card-article__cat">
            <?php endif; ?>
        
            <?php the_post_thumbnail( 'large', array( 'class' => 'u-w-100' ) ); ?>
          </div>
          <p><?php the_title(); ?></p>
        </a>
      </div>		
    
      <?php endwhile; ?>
    <?php endif; ?>
    
    <?php wp_reset_postdata(); ?>
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.