Support

Account

Home Forums Add-ons Repeater Field Output only first item in repeater field

Solved

Output only first item in repeater field

  • Hello –

    on my wordpress blog pages (index and single post) I have to do the following:

    1. On the index page show a thumbnail
    2. On the single post page show a slideshow (built using repeater field):

    <?php if( have_rows('slideshow') ): ?>
    <?php while( have_rows('slideshow') ): the_row(); ?>
       <?php the_sub_field('slideshow_image'); ?>
    <?php endif; ?>	
    <?php endwhile; ?>

    I want to be able to use the first item in the slideshow as the thumb on the index page.

    Any ideas as to how to output this?

    ๐Ÿ™‚

  • Start start doing a normal loop for a repeater and then break after the first row.

    
    if (have_rows('repeater')) {
      while (have_rows('repeater')) {
        the_row();
        // code here to show image
        
        // exit loop after first image
        break;
      }
    }
    
  • Answering my own question!

    Found answer on this page: https://www.advancedcustomfields.com/resources/repeater/ under sub-heading: “Get the first row from a repeater“.

  • Hi John,

    your solution was a way lot simpler than mine and almost worked –

    but I found that the second slide was displayed, not the first! ๐Ÿ™

    Any ideas to tweak the code so that it shows just the first one?

    Thanks anyway!

    Chris

  • here’s the code I’m using:

    <?php if( have_rows('news_slideshow') ): ?>
       <?php while ( have_rows('news_slideshow') ) : the_row(); ?>
    	<?php the_row();  ?>
    	<div>
    		<img src="<?php the_sub_field('news_slideshow_image'); ?>" alt="">
    	</div>
    	<?php break;  ?>
       <?php endwhile; ?>
    <?php endif; ?>
  • John – fixed it – my fault!

    I repeated ‘the_row();’ twice!

    <?php while ( have_rows(‘news_slideshow’) ) : the_row(); ?>
    <?php the_row(); ?>

    All good now.

    Thanks very much for your help.

    Chris

  • Hello i have same Question.

    break; will be ignored, someone an idea why:

    
    <!-- Preload First Slider Image Desktop -->
    <?php while ( have_rows( 'slider' ) ) : the_row(); ?>
    <?php if ( get_row_layout() == 'slider_inhalt' ) : ?>
    <?php if ( have_rows( 'slide_content' ) ) : $id = 0;  ?>
    <?php while ( have_rows( 'slide_content' ) ) : the_row(); $id++; ?>
    <?php the_row();  ?>
    <?php if ( get_sub_field( 'slider_image_desktop') ) { ?>
    <link rel="preload" href="<?php the_sub_field( 'slider_image_desktop' ); ?>" as="image" media="(min-width: 1000px)">
    <?php } ?> 
    <?php break;  ?>
    <?php endwhile; ?>
    <?php endif; ?>  
    <?php endif; ?>
    <?php endwhile; ?>
    
  • You should not use break inside of an acf repeater loop. Instead you should use a counter. Repeaters should always be allowed to loop over all rows.

    
    $counter = 0;
    while (have_rows('repeater')) {
      the_row();
      if ($counter) {
        // already did output of first, skip the rest
        continue;
      }
      // output first row here
      // increment counter so that no additional rows are output
      $counter++;
    }
    
  • sorry did not understand, the code gives me error.
    i have tried

    <?php if ( have_rows( 'slider' ) ): ?>
    <!-- Preload Slider Image Desktop -->
    <?php while ( have_rows( 'slider' ) ) : the_row(); ?>
    <?php if ( get_row_layout() == 'slider_inhalt' ) : ?>
    <?php if ( have_rows( 'slide_content' ) ) : $id = 0;  ?>
    <?php $counter = 0; ?>
    <?php while ( have_rows( 'slide_content' ) ) : the_row(); $id++;  ?>
    
    <?php if ($counter) {
        continue;
      } ?>
    <link rel="preload" href="<?php the_sub_field( 'slider_image_desktop' ); ?>" as="image" media="(min-width: 1000px)">
    <?php $counter++;
    } ?>
    
    <?php endwhile; ?>
    <?php endif; ?>  
    <?php endif; ?>
    <?php endwhile; ?>
    <!-- // Preload Slider Image Desktop // -->
    <?php else: ?>
    <?php  ?>
    <?php endif; ?>
    
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.