【问题标题】:PHP double loop problems in WordpressWordpress 中的 PHP 双循环问题
【发布时间】:2013-05-15 13:44:00
【问题描述】:

首先,我使用的是 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; 

?>

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

非常感谢任何帮助。

【问题讨论】:

    标签: php html wordpress loops if-statement


    【解决方案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>';
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多