PHP:中断循环,然后继续吗? [英] PHP: Break loop and then continue?

查看:101
本文介绍了PHP:中断循环,然后继续吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WordPress帖子循环中遇到问题.我想破坏发布循环,在发布中添加一些HTML div标签.例如,我正在尝试在第一篇文章之后在文章周围添加一个div .wrap.因为是包裹帖子,所以应该只有一次.

I'm having a problem with the WordPress posts loop. I'd like to break down the post loop, to add some HTML div tags in the post. For example, I'm trying to add a div .wrap around the posts after first post. As it's to wrap the posts, so it should be only once.

问题在于,由于循环而重复了.wrap div.如何中断循环,仅将.wrap div添加一次,然后继续循环?

The problem is that the .wrap div is being repeated because of the loop. How can I break the loop, and add the .wrap div only once and then continue the loop?

这是我的代码:

while ($wp_query->have_posts()) :

    $wp_query->the_post(); 
    if($wp_query->current_post <= 1){ ?>
        <div class="x">
        .. post 1
        </div>
    <?php }
    if($wp_query->current_post > 1){ ?>
        <div class="wrap"> <!-- should be only once -->
            <div class="y">
            .. post 2
            </div>

            <div class="y">
            .. post 3
            </div>
        </div><!-- should be only once -->
    <?php } ?>

endwhile;

推荐答案

尝试一下

<?php
while ($wp_query->have_posts()) :
    $wp_query->the_post(); 
    if($wp_query->current_post <= 1){ ?>
        <div class="x">
        .. post 1
        </div>
        <div class="wrap"> <!-- should be only once -->
    <?php 
    $is_wrapped = 1;
    }
    if($wp_query->current_post > 1){ ?>
        <div class="y">
          .. post 2, then post 3, ...
        </div>
    <?php } 
endwhile;?>
<?php if (!empty($is_wrapped)){ ?>
        </div><!-- should be only once -->
<?php } ?>

这篇关于PHP:中断循环,然后继续吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆