Image gallery in repeater field using Advanced Custom Fields

So I’m using the plugin Advanced Custom Fields in WordPress. I’m using a repeater field and one of my sub fields is Rocks. It is important to note that the rocks field is an image gallery.

<?php the_sub_field('rocks_gallery'); ?>

The problem is that its returning “array, array, array”.
The gallery does have 3 images in it.

I need to somehow access these arrays. So, I can see that I’m dealing with an associative array. The documentation is here. http://www.advancedcustomfields.com/resources/gallery/

The examples on the documentation aren’t the same situation because they are not working within a loop that is specific to a repeater field.

I’ve tried this, but I get the same result still.

                                                     <?php $RockImages = the_sub_field('rock_gallery');
													 
													 
													 echo $RockImages[0];
													 
													 print_r($RockImages_array);
													 
													  ?>

I’m probably going to have to do a foreach loop to get each image, but I’m not sure where to start. Any ideas?

"The gallery field will return an array of image data. Each image is itself an array containing information such as title, alt, description, url and more.

For each image in the array, you can expect to see an array like this"
(From the documentation).

I’d suggest reading the documentation you linked as a start :wink:

Yes I figured that each image was it’s own array. What I don’t get is how to get the name of each array.

I’ve been able to get this to work just fine for field, but it’s a lot more tricky when working with a gallery that is in a repeater field

	 <?php 
																	
																
    $images = get_field('practice_image_gallery');
																	
	if( $images ): ?>
		<ul class="list-inline" style="clear:both;">																			     <?php foreach( $images as $image ): ?>
																				
                                                                                                                 
         <li>
                <a class="fancybox" rel="group" href="<?php echo $image['url']; ?>">
                <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
																					
			    <p><?php echo $image['caption']; ?></p>
		</li>
                                                                                
			<?php endforeach; ?>
			</ul>
                                                                                                                                                                                                           
<?php endif; ?>

I tried this, but no luck.

         <?php $RockClimbingImages = the_field('rock_climbing_gallery');
	global $RockClimbingImages;								   
													   ?>
														  
                                                          
          <section class="RockClimbingRouteGallery" style="margin-top:3em;">
                             <?php	if( $RockClimbingImages): ?>
						<ul class="list-inline" style="clear:both;">																			       
<?php foreach( $images as $image ): ?>
																				
                                                                                                                 
                   <li><a class="fancybox" rel="group" href="<?php echo $image['url']; ?>">                                        										 <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
																					
			<p><?php echo $image['caption']; ?></p>																	</li>
                                                                                
		<?php endforeach; ?>
		</ul>
                                                                        
                                                                        
                                                                        
		<?php endif; ?>
                                                        
                                                        
            </section><!--RockClimbingRouteGallery-->

So… each individual array wont have a name. As you’ve seen, the top level is itself an array; while technically this could be considered an array of objects, it’s strictly speaking a muldimensional array.

You dont need to decllare $RockClimbingImages a global. It’s in scope of everything you’re doing.

You’re… almost there, except that $images is not defined; try foreach($RockClimbingImages as $image) instead.

Something is missing

I tried this, but still it returns nothing! I added the loop so you could see the whole picture of what I’m doing

<?php if(get_field('details_of_each_climbing_route')): ?>  
<?php while(has_sub_field('details_of_each_climbing_route')): ?>


  <?php $RockClimbingImages = the_field('rock_climbing_gallery'); ?>
													  
                                                          
                             <section class="RockClimbingRouteGallery" style="margin-top:3em;">
                                                        	 <?php 	if( $RockClimbingImages): ?>
																		<ul class="list-inline" style="clear:both;">
																			<?php foreach( $RockClimbingImages as $image ): ?>
                                                                                                                 
                                                    			 <li>
                                                                                <a class="fancybox" rel="group" href="<?php echo $image['url']; ?>">
             <img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
																<p><?php echo $image['caption']; ?></p>
																				</li>
                                                                                
																			<?php endforeach; ?>
																		</ul>
                                                                        
                                                                        
                                                                        
															<?php endif; ?>
                                                        
                                                        
                                                        </section><!--RockClimbingRouteGallery-->
														                        <?php endwhile; ?>
                                 
                                 			 		
                                          
                                          
                                          			 <?php endif; ?>
                            
<?php wp_reset_query(); ?>

var_dump($RockClimbingImages); just after it’s been defined, show me what the object looks like, it may make referencing it easier for you.

So yes it is returning null. What’s interesting is that I tried var_dump($route_name); where I know that it is outputting information and I still get a result of null.

There is something about the an images gallery in a repeater field. An image gallery is very easy to work with if it’s a field and not a sub field (the_sub_field) in a repeater field.

I did this for $images = get_field('practice_image_gallery');

var_dump($images)

and got this array. I’m probably just using the wrong function like on that string thread that I created.

array (size=6)
  0 => 
    array (size=18)
      'ID' => int 339
      'id' => int 339
      'title' => string 'ClimbingImage' (length=13)
      'filename' => string 'ClimbingImage1.jpg' (length=18)
      'url' => string 'http://localhost/outdoorsandfitness/wp-content/uploads/2015/06/ClimbingImage1.jpg' (length=81)
      'alt' => string '' (length=0)
      'author' => string '1' (length=1)
      'description' => string '' (length=0)
      'caption' => string '' (length=0)
      'name' => string 'climbingimage-2' (length=15)
      'date' => string '2015-06-30 00:39:06' (length=19)
      'modified' => string '2015-06-30 00:39:06' (length=19)
      'mime_type' => string 'image/jpeg' (length=10)
      'type' => string 'image' (length=5)
      'icon' => string 'http://localhost/outdoorsandfitness/wp-includes/images/media/default.png' (length=72)
      'width' => int 4928
      'height' => int 3264
      'sizes' => 
        array (size=18)
          'thumbnail' => string 'http://localhost/outdoorsandfitness/wp-content/uploads/2015/06/ClimbingImage1-150x150.jpg' (length=89)
          'thumbnail-width' => int 150
          'thumbnail-height' => int 150
          'medium' => string 'http://localhost/outdoorsandfitness/wp-content/uploads/2015/06/ClimbingImage1-300x199.jpg' (length=89)
          'medium-width' => int 300
          'medium-height' => int 199
          'large' => string 'http://localhost/outdoorsandfitness/wp-content/uploads/2015/06/ClimbingImage1-1024x678.jpg' (length=90)
          'large-width' => int 640
          'large-height' => int 424
          'shop_thumbnail' => string 'http://localhost/outdoorsandfitness/wp-content/uploads/2015/06/ClimbingImage1-180x180.jpg' (length=89)
          'shop_thumbnail-width' => int 180
          'shop_thumbnail-height' => int 180
          'shop_catalog' => string 'http://localhost/outdoorsandfitness/wp-content/uploads/2015/06/ClimbingImage1-300x300.jpg' (length=89)
          'shop_catalog-width' => int 300
          'shop_catalog-height' => int 300
          'shop_single' => string 'http://localhost/outdoorsandfitness/wp-content/uploads/2015/06/ClimbingImage1-600x600.jpg' (length=89)
          'shop_single-width' => int 600
          'shop_single-height' => int 600
................

so at this point foreach’ing $images should get you what you want.

No. It’s slightly different when you are working with a field vs a gallery that is in a field, specifically in a repeater field.

Okay. ACF just responded to me and here is what the solution is

<?php $RockClimbingImages = get_sub_field('rock_climbing_gallery'); ?>

Since it was a sub field of a repeater field I needed to use get_sub_field vs get_field.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.