Support

Account

Home Forums General Issues Relationship Gallery issue

Solving

Relationship Gallery issue

  • Hey there!

    First off, can’t stress how much I love ACF, its totally changed the way I build things! I’m just stuck on a little thing!

    I’m kinda stuck trying to get data out of an Gallery field, thats nested in a couple of queries. However, I can get normal ACF field – its just the gallery one that comes up blank, so I’m not sure why.

    <?php
    		$args = array ( 'post_type' => 'school-category');             
    		$loop = new WP_Query( $args );
    		while ( $loop->have_posts() ) : $loop->the_post();
    	?>
    	
    	<div>
    
    	<?php 
    	
    		/* films is a realtionship ACF field */
    		$films = get_field('films');
    			
    		if( $films ): ?>
    		
    			<div class="image_data">
    			
    				<?php foreach( $films as $post): ?>
    					
    					<?php setup_postdata($post); ?>
    												
    					<?php if ( get_field('running_time') ) :
    						the_field('running_time');
    					endif; ?>
    					
    												
    					<?php if ( get_field('images') ) :
    						the_field('images');
    					endif; ?>
    												
    				<?php endforeach; ?>
    				
    			</div>
    			
    			<?php wp_reset_postdata(); ?>
    		
    		<?php endif; ?>
    	
    	</div>
    				
    		
    	<?php endwhile; ?>

    So the running_time field gives me a result, but not the images field.

    Anyone have any ideas why?

    Thanks!

  • Hi @theschtickler

    Are you trying to create a gallery by using the code the_field('images');?

    This is not possible.

    The gallery field will return an array of images – each being an array containing data associated to that image.

    You need to follow the documentation to understand how to loop through the images and render a gallery

    Thanks
    E

  • Hey Elliot,
    Sorry, I simplified that to make it easy to understand (but prob made it more confusing :B ). Here’s my full code:

    <?php
    	$args = array ( 'post_type' => 'school-category');             
    	$loop = new WP_Query( $args );
    	while ( $loop->have_posts() ) : $loop->the_post();
    ?>
    
    <div>
    
    <?php 
    
    	/* films is a realtionship ACF field */
    	$films = get_field('films');
    		
    	if( $films ): ?>
    	
    		<div class="image_data">
    		
    			<?php foreach( $films as $post): ?>
    				
    				<?php setup_postdata($post); ?>
    											
    				<?php if ( get_field('running_time') ) :
    					the_field('running_time');
    				endif; ?>
    				
    											
    				<?php if ( get_field('images') ) :
    					$images = get_field('images'); ?>
    					<img src="<?php echo $images[0]['url']; ?>" alt="<?php the_title(); ?>">
    				<?php endif; ?>
    									
    			<?php endforeach; ?>
    			
    		</div>
    		
    		<?php wp_reset_postdata(); ?>
    	
    	<?php endif; ?>
    
    </div>
    				
    <?php endwhile; ?>

    The issue is that the if (get_fields(‘images’)) returns a false, where as all over the rest of the site it returns true. I dumped all the post’s custom fields and could see that images was an empty array, where all the ACF fields returned their values.

    Any ideas? Thanks Elliot!

  • Hi @theschtickler

    Can you find the film ID, edit the film and make sure the gallery field contains a value?

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Relationship Gallery issue’ is closed to new replies.