Advanced Custom Fields display last three sub-repeater rows
Probably you should be using for
instead of while
. And consider the following algorithm:
1) Get the
last row from event_month
2) Count the number of events in that month
3) If the number of events are more than or equal to 3.
3.1) Get last 3 events and display them
4) Else count the number of remaing events (
3-<<events in last month>>
)4.1) Now get the second last row and repeat steps 2,3,4
So using the above logic your code should look something like:
<?php function getEvents($rows, $noOfEvents){ $resultArray = array(); if($rows && count($rows > 0)) { $events = $rows[count($rows)-1]['event']; $events = is_array($events) ? $events : array(); $eventCount = count($events); if($eventCount < $noOfEvents){ $noOfOtherEvents = $noOfEvents-$eventCount; array_pop($rows); $iterate = getEvents($rows,$noOfOtherEvents); $resultArray = array_merge($events,$iterate); } else{ $resultArray = array_slice($rows, 0-$eventCount, $eventCount); } return $resultArray;}$rows = get_field('event_month', 1263);if($rows) { $requiredEvents = getEvents($rows,3); //3 or how many ever last you want foreach($requiredEvents as $event){ var_dump($event); //this should have all you need like $event['event_title'],$event['event_day'],ect... }}
This may not be the answer that everyone was looking for on this one, but here's what I did as a work-around, which worked well enough for me.
I ended up resolving the issue outside of php, using css to select the last three list items. Here is what I used, worked great.
.connect-list-wrapper ul li{ display: none;}.connect-list-wrapper ul li:nth-last-child(-n+3) { display: block;}