Alfred Kwak Alfred Kwak - 1 month ago 18
PHP Question

ACF Repeater in Wordpress not showing images

I'm having trouble getting images shown with a repeater in Wordpress.
For some reason it only shows the alt text instead of the image itself?
example

the upper images are my thumbnail posts and are showing correctly.
Does anyone know whats causing this?

my code

<div class="row">
<div class="col-sm-6 left"><h1>COMMERCIAL</h1><br>
<div class="page-header">

<?php $mymovies = new WP_Query(array(
'post_type' => 'my_movies'
)); ?>

<?php while($mymovies->have_posts()) : $mymovies->the_post(); ?>
<div class="movie-colums">
<div class="thumbtitle group">
<div class="preview">
<?php the_post_thumbnail('thumbnail'); ?>
<h1><?php the_title(); ?><br>
<?php the_field('year'); ?>
<?php
// check if the repeater field has rows of data
if( have_rows('images_rep') ):
// loop through the rows of data
while ( have_rows('images_rep') ) : the_row();
// display a sub field value
the_sub_field('image');
endwhile;
else :
// no rows found
endif;
?>
</h1>
</div>

</div>
</div>
<?php endwhile; ?>
</div>


thanks in advance

Answer

Your PHP bit for the repeater stuff should be:

<?php if( have_rows('images_rep') ): ?>

    <?php while( have_rows('images_rep') ): the_row(); 
        $image = get_sub_field('image');
    ?>

        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>">

    <?php endwhile; ?>

<?php endif; ?>

It looks like your example is outputting the image object so just wrap the image URL in an image tag.

Ref: https://www.advancedcustomfields.com/resources/image/

Comments