This morning I needed a way to get the next three upcoming events from the HSC Calendar listed on the home page. Here’s how I got it to work.
I already had the calendar set up with some PHP to filter out events that happened before today (notes on how this was done). What I needed was a way to get a limited number of entries starting at that point.
Here’s the code to get the list of all future entries in a format we can include in some other page. The event title is linked to more information, and the date is printed after that, enclosed in a span with class “date” so we can format it however we like.
- <a href=”<$MTEntryPermalink$>”><$MTEntryTitle$> <$MTEntryDate format=”%B %e”$>
$now=date(“Ymd”);
$entry=date(“<$MTEntryDate format="%Y%m%d"$>“);
if ($now<=$entry) {
?>
} ?>
A simple loop gets us only the next three events. Create a new index template (“Upcoming events”) and make sure the filename as a .php extension. Paste this in (code added from the previous version is in bold):
- <a href=”<$MTEntryPermalink$>”><$MTEntryTitle$> <$MTEntryDate format=”%B %e”$>
$now=date(“Ymd”);
$entry=date(“<$MTEntryDate format="%Y%m%d"$>“);
if (($now<=$entry) && ($i<3)) {
?>
}
$i++; ?>
You can see the loop counter being initialized and incremented, but where’s the loop? It’s the MTEntries tag. Movable Type loops through the code inside those tags already; no need to add a PHP loop on top of that. Just change $i<3 to suit the number of events you need in your list, and include this file elsewhere in your site.