Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirejordansmith/36680db83a1d9abfbda9 to your computer and use it in GitHub Desktop.
Save hirejordansmith/36680db83a1d9abfbda9 to your computer and use it in GitHub Desktop.
How to organize and output events by date in a loop
<?php
// Example using a while loop (inside WordPress in this case)
$already_echoed = array()
while ( have_posts() ) : the_post();
if (!in_array($date, $already_echoed)) { //check if current date is in the already_echoed array
echo $date;
}
$already_echoed[] = $date; //store all dates in an array to check against.
// Example using a foreach loop
$already_echoed = array();
foreach ($events as $event) {
if (!in_array($event->date, $already_echoed)) {
echo $event->date . "<br />";
}
$already_echoed[] = $event->date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment