Reputation: 3118
First of all, I'm using Wordpress. I have a loop that displays a custom content type called events
. I have user accounts that get tied to this event in the backend and I have it setup to output however many events they are tied to on their author.php page (user profile page).
There are normally more than one event in a month and currently the code is setup to display the events in separate div
's. It looks similar to this:
MAY 2013
- 14 May 2013 Title of the event - City, State <link>
MAY 2013
- 17 May 2013 Title of the event - City, State <link>
JUN 2013
- 01 Jun 2013 Title of the event - City, State <link>
If there are more than one event in a month, I would like it to combine all of the events into one 'month' div
and display like so:
MAY 2013
- 14 May 2013 Title of the event - City, State <link>
- 17 May 2013 Title of the event - City, State <link>
JUN 2013
- 01 Jun 2013 Title of the event - City, State <link>
I can't figure out how to arrange my loops to get the desired output. Here is my code.
<?php
$currentAuthorId = $authorID; // gets the current authors ID of the user you are viewing
$today = getdate();
$args = array(
'post_type' => 'events',
'year' => $today['year'],
'order' => 'ASC'
);
$eventQuery = new WP_Query($args);
while( $eventQuery->have_posts() ) : $eventQuery->the_post(); // Query for the events
$eventMonth = date('M', strtotime($eventQuery->post->post_date));
$eventYear = date('Y', strtotime($eventQuery->post->post_date));
echo '<div class="schedule-month">'; // Opening div for a month
echo '<h3>'.$eventMonth.'<span class="blue">'.$eventYear.'</span></h3>';
echo '<ul>';
if(get_field('event_performers')) { // Using ACF (advanced custom fields) I check if there are performer/users tied to the events
while(has_sub_field('event_performers')) {
$performer = get_sub_field('performer');
$uid = $performer['ID'];
if( $currentAuthorId == $uid) { // Match the current author to only the events he/she is tied to
$eventDate = date('d M Y', strtotime($eventQuery->post->post_date));
$city = get_field('city');
$state = get_field('state');
echo '<li>'; // Loop through all events with that author and display
echo ' <span class="blue">'.$eventDate.'</span>';
echo ' <p>'.$eventQuery->post->post_title.' - </p>';
echo ' <span class="light">'.$city.', '.$state.'</span>';
echo ' <a href="'.$eventQuery->post->guid.'">view event<span class="arrow"></span>';
echo ' </a>';
echo '</li>';
}
}
}
echo '</ul>';
echo '</div>';
endwhile;
?>
I know I need to store a variable checking if it's the same month, but I don't know how to arrange it correctly to display what I need.
Any help is much appreciated.
Upvotes: 1
Views: 168
Reputation: 3925
$tempMonth = '';
$tempYear = '';
while( $eventQuery->have_posts() ) : $eventQuery->the_post(); // Query for the events
$eventMonth = date('M', strtotime($eventQuery->post->post_date));
$eventYear = date('Y', strtotime($eventQuery->post->post_date));
if($tempMonth != $eventMonth || $tempYear != $eventYear)
{
$tempMonth = $eventMonth;
$tempYear = $eventYear;
echo '<div class="schedule-month">'; // Opening div for a month
echo '<h3>'.$eventMonth.'<span class="blue">'.$eventYear.'</span></h3>';
echo '<ul>';
}
Upvotes: 1