Support

Account

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

Solved

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.

    Thanks

    • jarvis

    • October 8, 2013 at 6:38 pm

    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;
     endif;
    }
    

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

    Hope that helps!?

    • Elliot

    • October 9, 2013 at 9:49 am

    Hi @poncedesigner

    You can use a simple counter as suggested like so:

    <?php 
    
    $i = 0;
    
    if(get_field('repeater_field_name')): 
    
    	$i++; 
    
    	if( $i > 6 )
    	{
    		break;
    	}
    	
    	?>
     
    	<ul>
     
    	<?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; ?>
     
    	</ul>
     
    <?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>
    </div>
    <?php endwhile; ?>
    
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>
    
    • Elliot

    • January 20, 2014 at 9:52 am

    Hi @eaglejohn

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

    Thanks
    E

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

    • Elliot

    • January 21, 2014 at 5:35 pm

    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
    E

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

    • Elliot

    • January 22, 2014 at 10:29 am

    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.

    Thanks
    E

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.