php - 围绕分组的循环开始处的空结束 div

标签 php wordpress while-loop

我什至不知道要搜索什么,所以我尝试过的东西并不多,因为我被难住了。

这是循环,获取月份名称,然后对该月份内的事件进行分组。注释 //* Close the Div : PROBLEM 是问题所在。该代码执行了它应该执行的操作,但列表之前有一个空 div。

if ( have_posts() ) : 

    $current_month = '';

    while ( have_posts() ) : the_post();


            if( $current_month != eo_get_the_start( 'm' ) ) :

                    $month = eo_get_the_start('F Y');

                    //* Close the Div : PROBLEM this is where it echos an empty closing div before first month name
                    echo '</div><!-- /events-by-month -->'; 

                    //* Start new group
                    echo '<h3 class="event-month" id="'. $month . '">' . $month . '</h3>';

                    //* Open div around the events in this month
                    echo '<div class="events-by-month" style="border:1px solid green">'; //* inline style so I can see

            endif;

                $current_month = eo_get_the_start('m');

                eo_get_template_part( 'eo-loop-single-event' );


    endwhile;


    //* Genesis Numbered Pagination
    genesis_numeric_posts_nav();

    else:

        echo '<p>'. __( 'No Events', 'joyful' ) . '</p>';

endif;

最佳答案

您的 while 循环最初将循环一次,并在回显初始开始标记之前回显结束标记。

只需检查 $current_month 是否为空即可修复该问题。

echo !empty($current_month) ? '</div><!-- /events-by-month -->' : null;

关于php - 围绕分组的循环开始处的空结束 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47701109/

相关文章:

C;如果我在 while 循环内的 for 循环中放置一个 break

php - Apache PHP HTTP_USER_AGENT 和 get_browser 返回不正确的值

javascript - 如果重新打开选项卡则重新加载页面

php - 如何以简单的方式将新的 Jqueryui Autocomplete 与 PHP+MySQL 结合使用

javascript - 完全坚持将 JQuery 加载到 WordPress 站点

php - 有没有办法避免最后出现空的 else { }?

r - R中的并行while循环

javascript - 是否可以检测颜色是浅色还是深色?

javascript - 为什么wordpress网站显示jquery错误请求?

wordpress - 搜索结果的自定义布局