PHP:中断循环然后继续?
我对WordPress帖子循环有问题。我想打破post循环,在post中添加一些HTMLdiv标记。例如,我试图在第一篇文章之后添加一个div.wrap。因为它是用来包装柱子的,所以它应该只有一次 问题是.wrap div由于循环而被重复。如何中断循环,只添加一次.wrap div,然后继续循环 这是我的密码:PHP:中断循环然后继续?,php,html,wordpress,Php,Html,Wordpress,我对WordPress帖子循环有问题。我想打破post循环,在post中添加一些HTMLdiv标记。例如,我试图在第一篇文章之后添加一个div.wrap。因为它是用来包装柱子的,所以它应该只有一次 问题是.wrap div由于循环而被重复。如何中断循环,只添加一次.wrap div,然后继续循环 这是我的密码: while ($wp_query->have_posts()) : $wp_query->the_post(); if($wp_query->cur
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;
您可以只使用迭代器索引,如下所示:
$i = 0;
while (...) {
if ($i == 1) {
// actions you need to do only once
}
...
$i++
}
您可以只使用迭代器索引,如下所示:
$i = 0;
while (...) {
if ($i == 1) {
// actions you need to do only once
}
...
$i++
}
如果没有帖子,你会得到一个空帖子;如果只有一个帖子,你会得到一个空帖子。但这在你的情况下可能是可行的
编辑:示例
<?php
$wp_query = new WPDummy;
echo '<div class="x">';
while ($wp_query->have_posts()) :
$wp_query->the_post();
$wp_query->the_content();
if($wp_query->current_post <= 1){
echo '</div>', "\n",'<div class="y">';
}
endwhile;
echo '</div>';
// boilerplate
class WPDummy {
public $current = null;
public $current_post = 0;
public function __construct() {
$this->posts = array( array('content'=>'A'),array('content'=>'B'),array('content'=>'C'),array('content'=>'D') );
}
public function have_posts() {
return false!=current($this->posts);
}
public function the_post() {
$this->current_post+=1;
$this->current = current($this->posts);
next($this->posts);
}
public function the_content() {
echo '<span class="content">', $this->current['content'], '</span>';
}
}
印刷品
<div class="x"><span class="content">A</span></div>
<div class="y"><span class="content">B</span><span class="content">C</span><span class="content">D</span></div>
如果没有帖子,你会得到一个空帖子;如果只有一个帖子,你会得到一个空帖子。但这在你的情况下可能是可行的
编辑:示例
<?php
$wp_query = new WPDummy;
echo '<div class="x">';
while ($wp_query->have_posts()) :
$wp_query->the_post();
$wp_query->the_content();
if($wp_query->current_post <= 1){
echo '</div>', "\n",'<div class="y">';
}
endwhile;
echo '</div>';
// boilerplate
class WPDummy {
public $current = null;
public $current_post = 0;
public function __construct() {
$this->posts = array( array('content'=>'A'),array('content'=>'B'),array('content'=>'C'),array('content'=>'D') );
}
public function have_posts() {
return false!=current($this->posts);
}
public function the_post() {
$this->current_post+=1;
$this->current = current($this->posts);
next($this->posts);
}
public function the_content() {
echo '<span class="content">', $this->current['content'], '</span>';
}
}
印刷品
<div class="x"><span class="content">A</span></div>
<div class="y"><span class="content">B</span><span class="content">C</span><span class="content">D</span></div>
试试这个
<?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
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 } ?>
你不必中断循环是的,换行输出代码不循环。你不必中断循环是的,换行输出代码不循环。我的问题是只在循环中添加一次额外的div wrap。我的问题是只在循环中添加一次额外的div wrap。