4

This is a bit of a hard one to explain, but I will do my best. If you need any clarification just ask :)

I have a template that needs to display a list of 3 upcoming events, listed by date ascending (nearest first, farthest away last). I have this working with the following code:

{exp:channel:entries channel="news-article" dynamic="no" order_by="entry_date" sort="asc" category="1" sticky="no" show_future_entries="yes"}
    {if '{entry_date format="%Y%m%d"}' >= '{current_time format="%Y%m%d"}'}
            <article>
                <a href="{url_title_path='news'}">
                    <p>
                        {title}
                        <span class="time-block">{entry_date {long-date}}</span>
                    </p>
                </a>
            </article>
    {/if}
{/exp:channel:entries}

However, this shows all future events, not just the closest 3. I have tried adding limit="3" to the channel entries tag but as there are past events, the conditional is never true so nothing shows at all.

So I tried adding a PHP counter to it like so:

<?php $counter = 0; ?>
{exp:channel:entries channel="news-article" dynamic="no" order_by="entry_date" sort="asc" category="1" sticky="no" show_future_entries="yes"}
    {if '{entry_date format="%Y%m%d"}' >= '{current_time format="%Y%m%d"}'}
        <?php if ($counter<3): ?>
            <article>
                <a href="{url_title_path='news'}">
                    <p>
                        {title}
                        <span class="time-block">{entry_date {long-date}}</span>
                    </p>
                </a>
            </article>
            <?php $counter ++;
        endif; ?>
    {/if}
{/exp:channel:entries}

Parsing PHP on input still displays all results today or in the future, and parsing PHP on output displays nothing. Does anybody know how to get something like this working?

Thanks in advance!

2 Answers 2

4

This is easily accomplished using the start_on parameter.

If the start_on date/time should be current time use:

{exp:channel:entries 
    channel="events" 
    limit="3" 
    sort="asc" 
    start_on="{current_time format='%Y-%m-%d %H:%i'}" 
    show_future_entries="yes"
}

If the start_on date/time should be dynamic, enable PHP in the template and set it to be parsed on "input". This is the code you use:

<?php
    $current_time = ee()->localize->format_date('%Y-%m-%d %H:%i', ee()->localize->now - 3600);
?>

Change the 3600 in the php code to whatever number you need.

{exp:channel:entries 
    channel="events" 
    limit="3" 
    sort="asc" 
    start_on="<?php echo $current_time; ?>"
    show_future_entries="yes"
}
2
  • This is a really great solution thanks a lot!
    – Joe Beaver
    Nov 5, 2013 at 16:42
  • 1
    This answer just solved an unrelated issue for me - thank you
    – JKM
    Dec 11, 2013 at 2:46
3

Not a solution to your specific question, but a solution to your general problem (how to display only upcoming events):

This is exactly why I wrote EEvent Helper years ago. The idea is that you set events to expire at the end of their event date, allowing you to easily pull just past events, or just upcoming events.

{!-- Upcoming Events --}
{exp:channel:entries channel="events" show_future_entries="yes" sort="asc"}

{!-- Past Events --}
{exp:channel:entries channel="events" show_expired="yes" sort="desc"}

(The add-on is not required to do this, but it automates the setting of the expiration date, making it more client-friendly.)

3
  • Hi, thanks for the suggestion. I think I may have to do it this way unless anything else is suggested. The problem is that events and news come from the same channel but are just categorised differently (don't ask me why :P) hopefully this method will still work as intended.
    – Joe Beaver
    Nov 5, 2013 at 15:13
  • Then again this would still have the problem of not limiting to only 3 entries :(
    – Joe Beaver
    Nov 5, 2013 at 15:19
  • You can just use limit with this method - but Anna's solution fits your bill better, since you're mixing news and events in one channel. Nov 5, 2013 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.