PHP foreach仅在第一次迭代中显示值

时间:2015-01-01 21:03:43

标签: php arrays wordpress foreach

我在wordpress中有一个自定义帖子类型用于节目列表。我有一个自定义的日历插件,但我遇到的问题是从每个循环的帖子中删除所有值。

当我启动foreach并打印出所有值时,它会在第一次显示一切正常但在它之后的每次迭代都不显示所有值(例如内容)。

for ($i = 1; $i <= $cal->days_in_month; $i++) {
    echo '<li class="date_top"><span class="day">'.$i.'</span><ol>';
    foreach ($cal->events as $event) {
        if($i == date("j", $event['date_object']) && 
                $cal->month_selected == date("n", $event['date_object'])) {

print_r($event); 

        }
    }

    echo '</ol></li>';
}

这是我从帖子中获取数据的功能。

function load_month_events() {
        $query = $this->month_events();
        foreach ($query as $post) {
            $event = array();
            $event['title'] = $post->post_title;
            $event['date'] = get_post_meta($post->ID, 'start_date', true);
            $event['end_date'] = get_post_meta($post->ID, 'end_date', true);
            $event['permalink'] = get_permalink($post->ID);
            $event['date_object'] = to_date_object($event['date']);
            $content_post = get_post($post->ID);
            $event['content'] = $content_post->post_content;

            $shows_categories = wp_get_post_terms($post->ID, 'shows_category');
            $showseason_string = "";
            foreach($shows_categories as $category) {
                $showseason_string = $showseason_string . $category->slug . " ";
            }
            $event['showseason'] = $showseason_string;
            $this->events[] = $event; 
        }
        $this->duplicate_multiday();
        usort($this->events, "cmp");
    }

输出的一个示例(它正确地调用了当月的所有31天,并列出了当天发生的事件,但它没有提取内容和结束日期。)

1
Array ( [title] => A choirs line [date] => 20150101 [end_date] => 20150131 [permalink] => http://localhost:8888/shows/a-choirs-line/ [date_object] => 1420070400 [content] => A Choirs Line Content Goes Here [showseason] => 2014-2015-season )
2
Array ( [title] => A choirs line [permalink] => http://localhost:8888/shows/a-choirs-line/ [showseason] => 2014-2015-season [date] => 20150102 [date_object] => 1420156800 )

提前感谢您提供的任何指导!我觉得我以前遇到过这个问题,但我不记得我是怎么解决它的,而且此刻我正在撞墙。

编辑:修复方法是将数组字段添加到功能duplicate_multiday

function duplicate_multiday() {
        foreach($this->events as $event) {
            if($event['end_date'] && $event['date'] != $event['end_date']) {
                $date1 = new DateTime($event['date']);
            $date2 = new DateTime($event['end_date']);
            $interval = $date1->diff($date2);
            for($x=1; $x <= $interval->days; $x++) {
                $new_event = array();
                $new_event['title'] = $event['title'];
                $new_event['permalink'] = $event['permalink'];
                $new_event['content'] = $event['content'];
                $new_event['end_date'] = $event['end_date'];
                $new_event['showseason'] = $event['showseason'];
                error_log($event['showseason']);
                $new_event['date'] = date_format($date1->add(new DateInterval('P1D')), "Ymd");
                $new_event['date_object'] = to_date_object($new_event['date']);
                $this->events[] = $new_event;
            }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以修改复制活动的$this->events self::duplicate_multiday()中的content数据,但不要复制某些信息,例如&#34; {{1}}&#34;您丢失的数组字段。