Support

Account

Home Forums Add-ons Gallery Field Get gallery within a relationship field

Solved

Get gallery within a relationship field

  • I have pages containing a relationship field to a custom post type (properties). On each property I have a custom gallery field. I’m trying to figure out on the front end, how to get the get the gallery from within the related post. Here’s my code attempt with a chunk I know is missing – but don’t know what to put in there. `<?php

    $locations = get_field(‘property_selection’); // this just returns the id of the property post
    $gallery = get_field(‘property_gallery’);

    if( $locations ): ?>

      <?php foreach( $locations as $location ): ?>

    • <?php echo get_the_title( $location );

      ?>

      <?php foreach( $gallery as $item ):

      ?>

    • <?php echo $item[‘url’]; ?>
    • <?php endforeach; ?>

      <?php endforeach; ?>

    <?php endif; ?>

  • Never mind I managed to resolve this – here’s my solution if anyone is looking for similar:

    <?php 
    
    $posts = get_field('property_selection');
    
    if( $posts ): ?>
    	<ul>
    	<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
    		<?php $gallery = get_field('property_gallery', $p->ID); ?>
    	    <li><?php echo get_the_title( $p->ID ); ?></li>
    
    	<?php if($gallery) : foreach( $gallery as $item ): // variable must NOT be called $post (IMPORTANT) ?>
    	    <li><?php echo $item['url']; ?> </li>
    	<?php endforeach; endif; ?>
    
    	<?php endforeach; ?>
    	</ul>
    <?php endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Get gallery within a relationship field’ is closed to new replies.