Created
February 12, 2016 20:32
-
-
Save hirejordansmith/36680db83a1d9abfbda9 to your computer and use it in GitHub Desktop.
How to organize and output events by date in a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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