Support

Account

Home Forums Add-ons Repeater Field Get galleries in repeater from Gutenberg Block

Helping

Get galleries in repeater from Gutenberg Block

  • Hi,

    I’m working on my personal portfolio site, and for the individual project pages I want to be able to create a flexible number of galleries in a repeater, that I can then add one by one to the page with help of Gutenberg blocks.

    I have the fields added and the block all set up, but for some reason I can just not manage to get the galleries.

    This is what I have so far, based on an old example found on the forum:

    <!-- CHECK OUT REPEATER "project_galleries" -->
    <?php if( have_rows('project_galleries') ): ?>
    
    <!-- CHECK TO SEE IF IT HAS ROWS IN IT -->
    <?php while ( have_rows('project_galleries') ) : the_row(); ?>
    
      <!-- DISPLAY THE TEXT-FIELD THEN ASSIGN OUR GALLERY TO VARIABLE $images -->
      <?php
    
      $images = get_sub_field('project_gallery'); 
    
      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
    	echo 'Nothing found'
    endif; ?>

    I would really appreciate any help on this!

  • Check that the “project_galleries” field in ACF is set up as a REPEATER, and inside it, “project_gallery” is configured as a gallery.

    To work with Gutenberg blocks, you can try using the “get_field()” function instead of “have_rows()”. Try this:

    <?php 
    $galleries = get_field('project_galleries');
    if($galleries): 
        foreach($galleries as $gallery):
            $images = $gallery['project_gallery'];
            if($images):
                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;
            endif;
        endforeach;
    endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.