Home › Forums › Add-ons › Repeater Field › 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
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!?
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; ?>
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
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 😉
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
The topic ‘Get "x" numbers of sub fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.