首先,我正在使用Wordpress。我有一个循环显示名为events
的自定义内容类型。我有后端绑定到此事件的用户帐户,我设置输出他们在author.php页面(用户个人资料页面)上绑定的许多事件。
一个月内通常会有多个事件,目前代码设置为在单独的div
中显示事件。它看起来与此相似:
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>
如果一个月内有多个活动,我希望将所有活动合并为一个'月'div
并显示如下:
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>
我无法弄清楚如何安排我的循环以获得所需的输出。这是我的代码。
<?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;
?>
我知道我需要存储一个变量检查是否是同一个月,但我不知道如何正确安排它以显示我需要的内容。
非常感谢任何帮助。
答案 0 :(得分:1)
$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>';
}