1

首先,我使用的是 Wordpress。我有一个显示自定义内容类型的循环,称为events. 我在后端有与此事件相关联的用户帐户,并且我将其设置为在其 author.php 页面(用户个人资料页面)上输出与它们相关联的许多事件。

一个月内通常有多个事件,目前代码设置为在单独div的 's 中显示事件。它看起来类似于:

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; 

?>

我知道我需要存储一个变量来检查它是否是同一个月,但我不知道如何正确排列它以显示我需要的内容。

任何帮助深表感谢。

4

1 回答 1

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>';
     }
于 2013-05-20T22:20:08.033 回答