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.

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

报告相同问题?