每 3 个 div 包裹在一个 div 中



I 使用 ACF Pro 输出数据。我不需要超过 3 个带有类"块"的div 元素显示在带有类"in"的div 中。

刚开始在wordpress中使用高级自定义字段,PHP非常糟糕,但我正在学习(。我为我糟糕的英语道歉。

这是我的循环:

<div class="in">
<?php if( have_rows('fourth-section_tariffs') ): ?>
<?php while( have_rows('fourth-section_tariffs') ): the_row(); 
$tariffs_number = get_sub_field('fourth-section_tariffs_number');
$tariffs_title = get_sub_field('fourth-section_tariffs_title');
$tariffs_cost = get_sub_field('fourth-section_tariffs_cost');
$tariffs_sale_text = get_sub_field('fourth-section_tariffs_sale_text');
?>
<div class="block">
<p class="title"><?php echo $tariffs_number; ?></p>
<?php if ( get_sub_field( 'fourth-section_tariffs_sale' ) == 1 ) { ?>
<span class="sale"><?php echo $tariffs_sale_text; ?></span>
<?php
} else {
} ?>
<h3><?php echo $tariffs_title; ?></h3>
<div class="price"><?php echo $tariffs_cost; ?></div>
<ul>
<?php if( have_rows('fourth-section_tariffs_marked') ): ?>
<?php while( have_rows('fourth-section_tariffs_marked') ): the_row(); 
$tariffs_marked_text = get_sub_field('fourth-section_tariffs_marked_text');
?>
<li><?php echo $tariffs_marked_text; ?></li>
<?php endwhile; ?>
<?php endif; ?>     
</ul>
</div>
<?php endwhile; ?>
<?php endif; ?> 
</div>

输出如下:

<div class="in">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>

并且需要这样:

<div class="in">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
<div class="in">
<div class="block"></div>
<div class="block"></div>
</div>

您正在打开和关闭带有"in"类的div,它会导致您的输出在开头有一个div,在结尾有一个结束。

要解决此问题,您需要为 inside while 循环传递div 创建,并创建一个计数器来检查计数器模块 3(计数器 %3(是否为 0。总是发生这种情况,div 将在开始时打印(并将关闭之前的div(。这里的代码:

<?php if( have_rows('fourth-section_tariffs') ): ?>
<?php 
$loop_counter = 0;
while( have_rows('fourth-section_tariffs') ): the_row(); 
if($loop_counter%3 == 0){
//if not is the first interaction we will first close the last openned that for open another after
if($loop_counter > 0){
?>
</div>
<?php
}
?>
<div class="in">
<?php
}
$tariffs_number = get_sub_field('fourth-section_tariffs_number');
$tariffs_title = get_sub_field('fourth-section_tariffs_title');
$tariffs_cost = get_sub_field('fourth-section_tariffs_cost');
$tariffs_sale_text = get_sub_field('fourth-section_tariffs_sale_text');
?>
<div class="block">
<p class="title"><?php echo $tariffs_number; ?></p>
<?php if ( get_sub_field( 'fourth-section_tariffs_sale' ) == 1 ) { ?>
<span class="sale"><?php echo $tariffs_sale_text; ?></span>
<?php
} else {
} ?>
<h3><?php echo $tariffs_title; ?></h3>
<div class="price"><?php echo $tariffs_cost; ?></div>
<ul>
<?php if( have_rows('fourth-section_tariffs_marked') ): ?>
<?php while( have_rows('fourth-section_tariffs_marked') ): the_row(); 
$tariffs_marked_text = get_sub_field('fourth-section_tariffs_marked_text');
?>
<li><?php echo $tariffs_marked_text; ?></li>
<?php endwhile; ?>
<?php endif; ?>     
</ul>
</div>
<?php 
$loop_counter++;
endwhile;
//close the last div
?>
</div>
<?php endif; ?>

我已经测试并像魅力一样工作。我创建了一个没有您的测试函数的沙箱,建议:http://sandbox.onlinephpfunctions.com/code/5dbdd02d887bf47eddcd208f41bb189c0e9fed97

最新更新