8

I am using Advanced Custom Fields (ACF) to pull repeater information from an events page and displaying a shortened list of the events on the home page.

I have set up a repeater to allow the user to input which month the event will take place in(Allowing them to put in multiple months of events), then a sub-repeater to allow them to add multiple events for the given month. Example below:

March

  • March 9th event
  • March 12th event
  • March 28th event

April

  • April 1st event
  • April 28th event

This is the current output on the events page, and it works as intended.

On the home page of the website, I need to pull the 3 newest(the event that is at the bottom of the list is the newest event) events and display them on the home page.

I am not having an issue with pulling and displaying the events on the home page. What I am having a problem with is displaying the events when the last three events(the child repeater) cross between months(the parent repeater).

Simply limiting the event output using a php loop across the if, while, statements only limits the number of events output in that month. My code I'm currently using on the home page, is below.

<?php if( have_rows('event_month', 1263)): ?>
<ul>
    <?php while ( have_rows('event_month', 1263) ) : the_row(); ?>
        <?php if( have_rows('event', 1263)):;   ?>
            <?php while ( have_rows('event', 1263) ) : the_row(); ?>
                <li>
                    <h3>
                        <a href="<?php echo esc_url( home_url( '/' ) ); ?>events/"><?php $summary = get_sub_field('event_title');
                            echo substr($summary, 0, 34),'...'; ?></a>
                            <span><?php the_sub_field('event_day_of_week');?>, <?php the_sub_field('event_sub_month');?> <?php the_sub_field('event_day');?></span>
                    </h3>
                </li>
            <?php endwhile; ?>
        <?php else: ?>
            <p>Show dates to be announced soon.</p><?php the_sub_field('event_title'); ?>
        <?php endif; ?>
    <?php endwhile; ?>
</ul>

What my desired output on the home page would look like if we capture the three latest events:

  • March 28th event
  • April 1st event
  • April 28th event
Bryan
  • 2,870
  • 24
  • 39
  • 44
Gordon Smith
  • 124
  • 13
  • Any reason to not just use a single repeater with a Date Entry field? You can then extract the month from it to play nicely with your code elsewhere on the site, and it would make sorting the most recent entries on the homepage a snap. – Leland Apr 05 '17 at 03:32
  • @Leland - I appreciate you asking as it is something I hadn't considered. Here is a link to the page displaying the events inside each month. http://ardentsolutionsllc.com/events/ If I were to only use one repeater, I wouldn't know how to associate the events with each month. The home page shows the list of events (bottom right) that I'm trying to shorten, to only display 3 of the latest events, which usually appear at the bottom of each month on the events page. – Gordon Smith Apr 05 '17 at 16:36
  • Okay, I'm re-reading your post – you want, on the homepage, the 3 most recently *added events*? Which is to say, you want on the homepage the 3 events that were created most recently in the WordPress admin? Or would you rather have the 3 events that are occurring the soonest to today? – Leland Apr 05 '17 at 17:25
  • @Leland I would like the three latest events that appear at the bottom of the events page(from the link above). Normally,I would use the method suggested by Elliott here https://support.advancedcustomfields.com/forums/topic/limiting-results-from-repeater-field/ but it won't work because some events may need to be pulled in across repeater months(sometimes example: 2 events posted in May would require 1 event being pulled from April to make 3 on the home page). – Gordon Smith Apr 05 '17 at 20:49
  • Hey, sorry for all the questions – I've got some code I could help with, but I just want to understand what you're looking for a little better. You want the "three latest events... at the bottom of the events page." In other words, you want the 3 events that are the furthest in the future? Is that correct? – Leland Apr 05 '17 at 21:01
  • @Leland Yes, that is correct. – Gordon Smith Apr 06 '17 at 15:49

2 Answers2

1

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... 
    }
}
Nishanth Matha
  • 5,993
  • 2
  • 19
  • 28
  • Thanks for your answer. I tried implementing your code into the site, but it broke the page stating that $requiredEvents => $event produces a syntax error 'unexpected '=>' (T_DOUBLE_ARROW)'. I don't know PHP well enough to resolve that issue. I could dig into it, but thought you'd be able to offer additional advice. – Gordon Smith Apr 05 '17 at 16:58
  • @GordonSmith that was a typo `=>` should be `as` refer to edit! And you need to improve your php skills! – Nishanth Matha Apr 06 '17 at 00:54
  • Thanks for the edit. I agree, and am continuing to improve daily. I'll give this a shot and report back. – Gordon Smith Apr 06 '17 at 15:50
0

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;
}
Gordon Smith
  • 124
  • 13