Home › Forums › General Issues › 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?
<?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[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?
Hi @evanrichards,
Checkout this WordPress forum topic => http://wordpress.org/support/topic/wp_get_attachment_image-no-longer-returns-title-attribute
Thanks @acf-support for the link. I’ve fixed it now thanks to your help!
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.
Thanks
E
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.