wordpress WP_Query循环中的无限循环

时间:2016-07-21 14:41:59

标签: php wordpress

我有以下代码,我不确定在while循环中发生的事情的上下文非常重要。问题在于if语句中的continue;。 if中的条件已满足,因此达到了continue;。这创造了一个无限循环。我不明白为什么会这样呢?

有人可以建议为什么WordPress无法在WP_Query循环中处理continue;吗?

while ($latest_events->have_posts()) {

        $id = get_the_ID();             
        $custom_fields = base_get_all_custom_fields($id);           
        $event_type = $custom_fields["course/event_type"][0];

        if(isset($event_type) && strpos_arr($event_type, array('Training')) !== false ){
                continue;
        }  

        $latest_events->the_post();
        get_template_part('partials/latest-espresso-events');   
 }

1 个答案:

答案 0 :(得分:1)

如果你不打电话给$latest_event->the_post(),循环计数器就不会前进,因此你将有一个无限循环。

您必须在$latest_event->the_post()语句前致电continue;以确保转至下一篇文章(否则$latest_events->have_posts()将始终返回TRUE)。

        if(isset($event_type) && strpos_arr($event_type, array('Training')) !== false ){
            $latest_event->the_post();
            continue;
        }  
相关问题