Home Forums Add-ons Repeater Field Get "x" numbers of sub fields


Get "x" numbers of sub fields

  • Hi, ¿Theres is any way to get “x” numbers of sub fields? for example i have a repeater field and the user add “X” fields but i only want to show in the front end the 6 first rows.

    i found this

    $rows = get_field('repeater_field_name' ); // get all the rows
    $first_row = $rows[0]; // get the first row
    $first_row_image = $first_row['sub_field_name' ]; // get the sub field value 

    but i dont know how to use to show text, not images, and also i dont know how to limit to show only “x” rows.


  • Hi

    I use if (++$i == 2) break; to stop the loop at 2 rows. EG.

    if( $rows ) {
     $i = 0;
     foreach( $rows as $row ) {
      if (++$i == 2) break;

    So you can change the 2 to a 6 to limit the number of values returned.

    Hope that helps!?

  • Hi @poncedesigner

    You can use a simple counter as suggested like so:

    $i = 0;
    	if( $i > 6 )
    	<?php while(has_sub_field('repeater_field_name')): ?>
    		<li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
    	<?php endwhile; ?>
    <?php endif; ?>
  • I’m trying to get this work with a repeater in a repeater, but no luck so far. Any suggestions?

    <?php if(get_field('agenda-month')): ?>
    <?php while(has_sub_field('agenda-month')): ?>
    <?php $i = 0; if(get_sub_field('agenda-item')): $i++; if( $i > 6 ) { break; } ?>
    <?php while( has_sub_field('agenda-item') ): ?>
    <div class="agenda-item">
    <span class="agenda-date"><?php $date = get_sub_field('agenda-dag'); $y = substr($date, 0, 4); $m = substr($date, 4, 2); $d = substr($date, 6, 2); $time = strtotime("{$d}-{$m}-{$y}"); echo date_i18n( 'j-m', $time ); ?></span>
    <span><?php the_sub_field('agenda-text'); ?></span>
    <?php endwhile; ?>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>
  • Hi @eaglejohn

    Can you please elaborate on what is and what isn’t working?


  • The repeater in the repeater itself is working, I’m just getting all the rows instead of the first six that I would like.

  • Hi @eaglejohn

    The code $i++; if( $i > 6 ) { break; } should be WITHIN the while loop, not within the if statement.

    Please also note that for the $i to work correctly, it must be placed BEFORE the while loop.


  • Thanks, got it working! Partially though, because right now I’m getting the first six of the second repeater. And because it’s the second repeater I’m getting chunks of 6 items repeatedly 😉

    But I’m fine with it for now. I’m setting all items to display: none and setting the first six to display: block. Not the most ideal situation but my clients budget already reached its limit 😉

  • Hi @poncedesigner

    I am not sure of which repeater you want the first 6 rows from. If you want the first 6 rows of the 1st repeater, place your $i BEFORE the first while statement and the $i++ INSIDE the first while statement.

    if you want the first 6 rows of each sub repeater, move the code to the relevant while statement.


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

The topic ‘Get "x" numbers of sub fields’ is closed to new replies.