Support

Account

Home Forums General Issues Nested Relationship Fields

Solved

Nested Relationship Fields

  • For my home page I have the relationship ACF “featured_listings” which pulls my Custom Post Type Properties, right now I have 2 featured on the home page but only one appears. However when i take out the nested relationship “agent” it works fine, so I know its an issue with the nested field “agent” I just cant figure out how to fix it. Note: “agent” is a relationship field on the indv property post. Thank you in advance!

    <?php $posts = get_field('featured_listings'); if( $posts ): ?>
            <div class="jcarousel-wrapper">
                <div class="jcarousel">
                    <ul>
    				<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    					<?php setup_postdata($post); ?>
    				    <li class="listing">
    						<h1><a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
    
    					<?php $posts = get_field('agent'); if( $posts ): ?>
    						<div class="agents">
    						<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
    						    <div class="agent">
    						    	<a href="<?php echo get_permalink( $p->ID ); ?>">Agent: <?php echo get_the_title( $p->ID ); ?></a>
    						    	<?php if(get_field('phone_number', $p->ID)) : ?><div class="phone-number"><?php the_field('phone_number', $p->ID); ?></div><?php else: ?><?php endif; ?>
    						    	<?php if(get_field('email', $p->ID)) : ?><div class="email"><?php the_field('email', $p->ID); ?></div><?php else: ?><?php endif; ?>
    						    </div>
    						<?php endforeach; ?>
    						</div>
    					<?php endif; ?>
    
    				    </li>
    				<?php endforeach; ?>
                    </ul>
                </div>
                <a href="#" class="jcarousel-control-prev"></a>
                <a href="#" class="jcarousel-control-next"></a>
            </div>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
  • figured out the issue, i had to change the following code from…

    <?php $posts = get_field('agent'); if( $posts ): ?>
    						<div class="agents">
    						<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>

    to this code….. basically $posts in this section to $agent

    <?php $agent = get_field('agent'); if( $posts ): ?>
    						<div class="agents">
    						<?php foreach( $agent as $p ): // variable must NOT be called $post (IMPORTANT) ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Nested Relationship Fields’ is closed to new replies.