Reputation: 47
For a custom WP theme I am creating I am using ACF. I need to have a repeater that outputs images. I have it set to image object and using the correct code. In fact I tried it without a repeater and it works perfectly. Its only within the repeater that it fails.
<div class="row col-md-12">
<?php
// check if the repeater field has rows of data
if( have_rows('pics') ):
// loop through the rows of data
while ( have_rows('pics') ) : the_row();
// display a sub field value
?><div class="col-md-4">
<?php
$image = get_field('img');
if( !empty($image) ): ?>
<img src="<?php echo $image; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div> <?
endwhile;
else :
// no rows found
endif;
?>
</div>
What is causing the image data to not loop?
Upvotes: 1
Views: 10826
Reputation: 707
I think this is what you have to do:
<?php
// check if the repeater field has rows of data
if( have_rows('img') ):
// loop through the rows of data
while ( have_rows('img') ) : the_row();
// display a sub field value
$image = get_sub_field('sub_img'); ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endwhile;
else :
// no rows found
endif;
?>
This is assuming that you are outputting an image object and not a url in your ACF settings.
Upvotes: 4
Reputation: 6777
Your code looks ok, but having checked how I did it I believe you should be using get_sub_field
instead of get_field
http://www.advancedcustomfields.com/resources/get_sub_field/
Your comment about using the ['url'] part of the array is also relevant. This is working for me;
$image = get_sub_field('img');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
Upvotes: 4