Home Forums General Issues Get the image using image ID in repeater


Get the image using image ID in repeater

  • This is a simple question really but my novice php skills don’t extend this far. I have a repeater field and within that I have an image field. I’m trying to loop through each image instance and get the url of the image and its title. I have also checked Image ID for ‘gimages’ in the backend of ACF. What am I doing wrong?

    $img = the_sub_field('gimages');
    if(get_field('image_gallery')): ?>
        <?php while(has_sub_field('image_gallery')): ?>
            <a href="<?php echo $img["url"] ?>" title="<?php echo $img["title"] ?>">Link to image here</a>
        <?php endwhile; ?>									 
    <?php endif; ?>
  • You’ll need to use wp_get_attachment_image if your using the ID – maybe something like below – havnt tested but hopefully it’ll work

    if(get_field('image_gallery')): ?>
        <?php while(has_sub_field('image_gallery')): ?>
    $attachment_id = get_sub_field('illustrations_image');
    $size = "thumbnail"; // (thumbnail, medium, large, full or custom size)
    $imageurl = wp_get_attachment_image_src( $attachment_id, 'full' );
    $image_title = $attachment->post_title;
            <a href="<?php echo $imageurl[0] ?>" title="<?php echo $image_title; ?>"><?php echo $img; ?></a>
        <?php endwhile; ?>									 
    <?php endif; ?>
  • Thanks for your code @Jamie/@pomponian, I’ve tested it and the image is being pulled through however the title still is not. Any ideas why not?

  • Thanks @acf-support for the link. I’ve fixed it now thanks to your help!

  • Hi @evanrichards

    Going back to the original code, there are quite a few syntax errors. Firstly, can you please read over the repeater field documentation to better understand how the has_sub_field to have_rows loops work?

    I think @Jamie code will work for you, however, the sub field name looks wrong. Just change $attachment_id = get_sub_field('illustrations_image'); to your field name $attachment_id = get_sub_field('gimages');

    I am amusing that gimages is your sub field name.

    Perhaps you could also post a screenshot of the edit page showing the repeater field and it’s sub fields? Also, please list the correct field names.


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

The topic ‘Get the image using image ID in repeater’ is closed to new replies.