0

Im currently showing some data from a loop and loading a template:

<?php $index = 0; if ( have_rows('sections', get_option('page_for_posts') ) ) : while ( have_rows('sections', get_option('page_for_posts')) ) : the_row(); $index++; ?>
    <?php get_template_part('parts/section', get_row_layout() ); ?>
<?php endwhile; endif; ?>

Within the template how do I make a loop count depending on each loop of image? - I currently have:

<?php if ( $image = get_sub_field('image') ) : ?>
    <div class="inner">
        Image [loopcount]
    </div>
<?php endif; ?>

So lets say 3 images exists from in the loop, then im looking to print out:

<div class="inner">
    Image 1
</div>
<div class="inner">
    Image 2
</div>
<div class="inner">
    Image 3
</div>
1
  • This would be related to ACF. get_sub_field is an ACF function and the logic depends is their own. So I would recommend checking with their documents/forum to see if you could get the image count from it.
    – bynicolas
    Aug 29 '16 at 15:01
0

You can get images with foreach loop but first you have to print_r($images) and then get the value of the array something like this $images['image'] or $images['src'] or $images['url'] etc. But first check is there any record where you are getting get_sub_field('image') all images.

<?php if ( $images = get_sub_field('image') ) :
    print_r($images);
    foreach($images as $image) {
?>
    <div class="inner">
        <?php
            echo $image;
        ?>
    </div>
<?php } endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.