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?
<?php $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
<?php if(get_field('image_gallery')): ?> <?php while(has_sub_field('image_gallery')): ?> <?php $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 ?>" title="<?php echo $image_title; ?>"><?php echo $img; ?></a> <?php endwhile; ?> <?php endif; ?>
Checkout this WordPress forum topic => http://wordpress.org/support/topic/wp_get_attachment_image-no-longer-returns-title-attribute
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.
The topic ‘Get the image using image ID in repeater’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.