如果我在一个未知的迭代中存在循环,那么您如何确保上次运行后最后一个div被关闭?我不确定我的逻辑是否正确,是否还有更有效的方法。
例如,我需要6个“ sub-col”(可能在另一个szenario中,我只需要4个或3个“ sub-col”)。
循环可以完全随机地运行。
<?php
$loop_iteration = 0;
while ( have_posts() ) : the_post();
if ( $loop_iteration % 6 == 0 ) {
if ( $loop_iteration != 0 ) { echo '</div> <!-- sub-inner -->';echo '</div> <!-- sub-row -->'; }
echo '<div class="sub-row row">';
echo '<div class="sub-inner">';
}
echo '<div class="sub-col">';
/* do stuff */
echo '</div>';
$loop_iteration++;
endwhile;
// here I also could make a function, but for demonstration I let it as 'if'
if (
$loop_iteration % 6 == 1
|| $loop_iteration % 6 == 2
|| $loop_iteration % 6 == 3
|| $loop_iteration % 6 == 4
|| $loop_iteration % 6 == 5
) {
echo '</div> <!-- sub-inner -->';
echo '</div> <!-- sub-row -->';
}
?>
相同的循环,不同的检查
<?php
$loop_iteration = 0;
$div_status = 'close';
while ( have_posts() ) : the_post();
if ( $loop_iteration % 6 == 0 ) {
if ( $loop_iteration != 0 ) { echo '</div> <!-- sub-inner -->';echo '</div> <!-- sub-row -->'; }
echo '<div class="sub-row row">';
echo '<div class="sub-inner">';
$div_status = 'open';
}
echo '<div class="sub-col">';
/* do stuff */
echo '</div>';
$loop_iteration++;
endwhile;
if ( $div_status == 'open' ) {
echo '</div> <!-- sub-inner -->';
echo '</div> <!-- sub-row -->';
}
?>
谢谢。
答案 0 :(得分:0)
只需将打开和关闭的标签移出循环:
$loop_iteration = 0;
while ( have_posts() ) : the_post();
if ($loop_iteration !== 0) {
echo '</div> <!-- sub-inner -->';
echo '</div> <!-- sub-row -->';
}
echo '<div class="sub-row row">';
echo '<div class="sub-inner">';
echo '<div class="sub-col">';
/* do stuff */
echo '</div>';
$loop_iteration++;
endwhile;
if ($loop_iteration > 0) {
echo '</div> <!-- sub-inner -->';
echo '</div> <!-- sub-row -->';
}