Support

Account

Home Forums Add-ons Repeater Field Count in repeater field

Solved

Count in repeater field

  • Hi,

    i have an repeater field for images and try to set classes for one, two or three images. will set the section layout if only 1 images to full width, 2 images (to grid-cols-2) and 3 images (to grid-cols-3)

    <section class="grid md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-4 mt-6">
                <?php 
                    if( have_rows('content_images') ) : while( have_rows('content_images')) : the_row(); 
                
                    $image = get_sub_field('content_image'); ?>
                    <a data-fancybox="gallery" class="feature-zoom hover:shadow-2xl" href="<?= $image; ?>" title="&copy; <?php the_sub_field('content_image_copyright'); ?>" data-caption="&copy; <?php the_sub_field('content_image_copyright'); ?>">
                        <img src="<?= $image; ?>" title="&copy; <?php the_sub_field('content_image_copyright'); ?>" alt="">
                    </a>
                <?php endwhile; endif; ?>
            </section>

    hope someone can help me

  • 
    <?php 
      $count = count(get_field('content_images'));
    ?>
    
  • hi john,

    thanks.

    function content_image($i)
        {
        ?>
            <?php 
                $count = count(get_sub_field('content_images'));  
                
                if( have_rows('content_images') ) :  
                      
                    if($count == 1) { ?>
                        <section class="grid gap-4 mt-6">
                    <?php }elseif($count == 2) { ?>
                        <section class="grid md:grid-cols-2 lg:grid-cols-2 xl:grid-cols-2 gap-4 mt-6">
                    <?php }elseif($count == 3) { ?>
                        <section class="grid md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-4 mt-6">
                    <?php } 
                    while( have_rows('content_images')) : the_row(); 
                        $image = get_sub_field('content_image'); ?>
                        <a data-fancybox="gallery" class="feature-zoom hover:shadow-2xl" href="<?= $image; ?>" title="&copy; <?php the_sub_field('content_image_copyright'); ?>" data-caption="&copy; <?php the_sub_field('content_image_copyright'); ?>">
                            <img src="<?= $image; ?>" title="&copy; <?php the_sub_field('content_image_copyright'); ?>" alt="">
                        </a>
                    <?php endwhile; ?>
                        </section>
                <?php endif; ?>
        <?php  
        }
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.