Support

Account

Home Forums General Issues Loading a field with a Relationship field within a Flex Content Field Reply To: Loading a field with a Relationship field within a Flex Content Field

  • Your remark got me thinking though, Elliot. And now I found the simple solution: rename the post object! The solution:

    <?php // If selection is made, load selection of customers
    $customers = get_sub_field('customers-select');
    if( $customers ): ?>
    	<?php foreach( $customers as $customer): // variable must be called $post (IMPORTANT) ?>
    		
    		<?php setup_postdata($customer); ?>
    
    		<?php $post_id = $customer->ID; ?>
    	    <?php $thumbid = get_post_thumbnail_id($post_id); ?>
    	    <?php $img = wp_get_attachment_image_src( $thumbid, 'medium' ); ?>
    	    <?php $logo = $img[0]; ?>
    	    <?php $customer_url = get_field('customer-url', $post_id); ?>
    
        	<li class="l-fifth customer">
        		<a href="<?php echo $customer_url ?>" role="img" alt="<?php the_title(); ?>" style="background-image:url(<?php echo $logo ?>);"></a>
        	</li>
    
    	<?php endforeach; ?>
    	<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;?>

    Thanks & sorry for the waste of time, I should’ve known since I already used this method quite a lot of times.