Support

Account

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

Solved

Loading a field with a Relationship field within a Flex Content Field

  • Hi. I have a site where I use quite (or too) much flexible content fields on every page. They’re the blocks the user can build his pages with.

        <?php // START CONTENT BLOCKS ?>
        <?php while(has_sub_field('blocks')): ?>
    
            <?php if( get_row_layout() == 'block-customers') : // layout: Klanten (selectie) ?>
                <?php if ( get_sub_field('block-hide') == false ) { ?>
                    <?php get_template_part('_/inc/block-customers'); ?>
                <?php } ?>
    
            <?php endif; ?> 
        <?php endwhile; ?>

    I left out all the other sub fields

    Now in one of those blocks there’s a relationship field with which the user can choose and order which customer’s logo’s to display.

    I load the relationship field as a post-object. And I use the ‘featured image’ for the image (customer’s logo) and the Post Title for the customer’s name.

    <div class="block block-customers">
    	<h2>Klanten</h2>
    
    	<ul class="customers">
    		<?php // If news selection is made, load selection of customers
    		$posts = get_sub_field('customers-select');
    		if( $posts ): ?>
    			<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    				<?php setup_postdata($post); ?>
    
    			    <?php $thumbid = get_post_thumbnail_id($post->ID); ?>
    			    <?php $img = wp_get_attachment_image_src( $thumbid, 'medium' ); ?>
    			    <?php $logo = $img[0]; ?>
    			    <?php $customer_url = 'href="' . get_field('customer-url', $post->ID) . '"'; ?>
    
    		    	<li class="l-fifth customer">
    		    		<a <?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;?>
    	</ul><!-- /.customers -->
    </div><!-- /.block-customers -->

    But I also want to ad a URL to the logo’s. So I made a custom field in the custom post type ‘customers’ โ€“ with which we’re dealing here all the time. The problem is. That field, called ‘customer-url’, is not being loaded at all!

    The problem: I need to load that ‘customer-url’ per customer logo, but it won’t load! Not even when I call it like in the code above with the post->ID. So what am I missing / doing wrong?

    Is it because the parent flexible content field? If so.. How do I fix this? Any help is much appreciated!

    PS
    I added a screenshot of the custom field config in question.

  • I just found this post on the forum (I searched before already ๐Ÿ˜‰ ):
    http://support.advancedcustomfields.com/forums/topic/displaying-items-within-a-cpt-as-choices-in-repeater-for-another/

    It might be related to my problem. Do I understand correctly my only solution is to maybe use the ‘Dynamically populate a select fieldโ€™s choices’ solution? ๐Ÿ™

  • Hi @Bram Willemse
    The issue is not related to the nested situation of your relationship field. The issue is for some reason, the value is not being loaded from the ‘customer’ pos t object.

    Because your code sits within a separate file, it is possible that the $post->ID variable is not correct. You should output this within your loop and double check that the value matches for each customer.

    You may need to place this at the top of the file:

    
    global $post
    
  • Hi Elliot,

    Thanks for your quick reply. But I’m quite sure that would’nt work.

    The thing is, all this code is loaded on a page. The customer post objects are loaded in the foreach loop created with the relationship field. So the $post->ID variable should come out of the postdata within the foreach loop. If I would place it outside, the $post->ID from the page it’s all being load on would be used instead of the customer post objects.

    The weird thing is, the customer post objects $post->ID works for the post thumbnail:

    <?php $thumbid = get_post_thumbnail_id($post->ID); ?>

    But it doesn’t work when I call the custom field by that same $post->ID in the same foreach loop.

    <?php $customer_url = 'href="' . get_field('customer-url', $post->ID) . '"'; ?>

  • 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.

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

The topic ‘Loading a field with a Relationship field within a Flex Content Field’ is closed to new replies.