Support

Account

Home Forums Add-ons Gallery Field Gallery Field inside a Repeater field Reply To: Gallery Field inside a Repeater field

  • I figured it out. Here’s how to use galleries inside a repeater field:

    BASIC TIP: Do not use “get_field” or “the_field” inside a repeater. Instead use “the_sub_field” and “get_sub_field” instead.

    The following code will work assuming you:

    – Created a repeater called “gallery-repeater”.
    – Created a text field inside that repeater called “gallery-name”.
    – Created a gallery inside that repeater called “gallery-images”.

    
    <!-- CHECK OUT REPEATER "gallery-repeater" -->
    <?php if( have_rows('gallery-repeater') ): ?>
    
       <!-- CHECK TO SEE IF IT HAS ROWS IN IT -->
       <?php while ( have_rows('gallery-repeater') ) : the_row(); ?>
    
         <!-- DISPLAY THE TEXT-FIELD THEN ASSIGN OUR GALLERY TO VARIABLE $images -->
         <?php the_sub_field('gallery-name'); 
    
         $images = get_sub_field('gallery-images'); 
    
         if( $images ):?>
            <?php foreach( $images as $image ): ?>
    	   <a href="<?php echo $image['url']; ?>">
    	      <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
               </a>
               <p><?php echo $image['caption']; ?></p>
            <?php endforeach; ?>
         <?php endif; 
       endwhile;
    else :
    // no rows found
    endif; ?>

    It’s not styled so it’s really bare.

    ! Careful not to mix up PHP (if then else) code and HTML code.