doujiu8479 2015-01-01 21:03
浏览 56
已采纳

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

I have a custom post type in wordpress for show listings. I have a custom calendar plugin I put together but am having issues pulling all the values from a post over in my for each loop.

When I start the foreach and print out all values, it shows everything fine the first time around but every iteration after it does not display all of the values (such as content).

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>';
}

Here is my function to grab the data from the post.

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");
    }

And an example of the output (it is calling all 31 days of the month correctly and listing the events that are on that day but it's not pulling in the content and end date. )

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 )

Thanks in advance for any guidance you can provide! I feel like I have ran into this before but I can't remember how I solved it and I am banging my head into a wall at this point.

edit: The fix was to add the array fields to the funtion 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条回答 默认 最新

  • duankekan9269 2015-01-01 21:11
    关注

    You modify the data of $this->events in self::duplicate_multiday() in which you duplicate events, but you do not copy some of the information like the "content" array field you're missing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法