Support

Account

Home Forums General Issues Post Object / foreach creating 24 duplicates

Solving

Post Object / foreach creating 24 duplicates

  • I have a Group that contains 2 Text fields and 1 Repeater. And inside the Repeater is a Post Object. Like this:

    
    Our Partners / 'our_partners' (Group)
      ∟ Title / 'title' (Text)
      ∟ Intro / 'intro' (Text)
      ∟ Partners / 'partners' (Repeater)
          ∟ Partner / 'partner' (Post Object - Filter by Post Type: Partner)
    

    The CPT (partner) which only has the normal WP page title and an ACF Image field (‘logo’).

    I can get the correct selections to display both the title and the image, but it also seems to be returning 23 other semi-empty DIVs on top.

    Here is my current code:

    
    <div class="row" id="our_partners">
    	<div class="col">
    
    		<?php $our_partners = get_field('our_partners'); if( $our_partners ): ?>
    			<h1 class="title"><?php echo $our_partners['title']; ?></h1>
    			<div class="centre"><?php echo $our_partners['intro']; ?></div>
    
    			<?php if( have_rows( 'our_partners' ) ): ?>
    				<?php while( have_rows( 'our_partners' ) ): the_row(); ?>
    
    					<div class="squares">
    
    						<?php while( have_rows( 'partners' ) ): the_row(); ?>
    							<?php $partner = get_sub_field('partner'); if( $partner ): ?>
    
    								<?php foreach( $partner as $post ): ?>
    									<div class="square">
    										<img src="<?php echo get_field( 'logo' ); ?>" alt="<?php echo esc_html( $partner->post_title ); ?>">
    									</div>
    								<?php endforeach; ?>
    
    							<?php endif; ?>
    						<?php endwhile; ?>
    
    					</div>
    
    				<?php endwhile; ?>
    			<?php endif; ?>
    
    		<?php endif; ?>
    
    	</div>
    </div>
    

    And here’s what it’s returning:

    
    <div class="squares">
    	<div class="square"><img src="https://THE_FIRST_IMAGE_PATH.png" alt="THE_FIRST_PAGE_TITLE"></div>
    	<div class="square"><img src="" alt="THE_FIRST_PAGE_TITLE"></div>
    	<div class="square"><img src="" alt="THE_FIRST_PAGE_TITLE"></div>
    	<div class="square"><img src="" alt="THE_FIRST_PAGE_TITLE"></div>
    	(and another 20 of these)
    	<div class="square"><img src="https://THE_SECOND_IMAGE_PATH.png" alt="THE_SECOND_PAGE_TITLE"></div>
    	<div class="square"><img src="" alt="THE_SECOND_PAGE_TITLE"></div>
    	<div class="square"><img src="" alt="THE_SECOND_PAGE_TITLE"></div>
    	<div class="square"><img src="" alt="THE_SECOND_PAGE_TITLE"></div>
    	(and another 20 of these)
    </div>
    

    So technically it’s working. I just need some assistance to get all the excess ones gone.

    Many thanks in advance for any help given.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.