Home › Forums › General Issues › Reverse order, get last row…
Hi,
I have a repeater field associated with users that outputs invoice totals (see attached).
I have it outputting in reverse order, but ideally I would like to only show the most recent/latest row (3, in this case).
<?php $current_user = wp_get_current_user(); ?>
<?php
$repeater = get_field('invoices', 'user_' . $current_user->ID);
$order = array();
foreach( $repeater as $i => $row ) {
$order[ $i ] = $row['invoice_number'];
}
array_multisort( $order, SORT_DESC, $repeater );
if( $repeater ): ?>
<ul>
<?php foreach( $repeater as $i => $row ): ?>
<li><?php echo $row['invoice_number']; ?>. <?php echo $row['invoice_total']; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Can anyone help?
// initialize a counter
$counter = 0;
foreach ($repeater as $i => $row) {
if ($counter == 3) {
break;
}
// output row
// increment counter
$counter++;
}
Thanks John,
Two questions;
Thanks
You initialize the counter before you’re loop. You put the if statement at the start of your loop. You increment the counter at the end of the loop.
This will show the first X items set by the if statement. Since you are reversing the order of the repeater this means it will show the last X items in reverse order. You can set X for whatever number you want to show. It will show up to that number, so if there are only 2 items then it would only show 2.
You must be logged in to reply to this topic.
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.