[英]While loop break after n number
I want to create new li after every 16th anchor tags in while loop. 我想在while循环中每隔16个锚标记后创建新的li。 For example if 48 anchor tags so li should divide into 3 sets of li, each with 16 anchor tags.
例如,如果48个锚标签,那么li应该分为3组li,每组有16个锚标签。
<li>
<a href="#" data-id="1"><img src="img/temp/1.jpg" /></a>
<a href="#" data-id="2"><img src="img/temp/2.jpg" /></a>
<a href="#" data-id="3"><img src="img/temp/3.jpg" /></a>
<a href="#" data-id="4"><img src="img/temp/4.jpg" /></a>
<a href="#" data-id="5"><img src="img/temp/5.jpg" /></a>
<a href="#" data-id="6"><img src="img/temp/6.jpg" /></a>
<a href="#" data-id="7"><img src="img/temp/7.jpg" /></a>
<a href="#" data-id="8"><img src="img/temp/8.jpg" /></a>
<a href="#" data-id="9"><img src="img/temp/1.jpg" /></a>
<a href="#" data-id="10"><img src="img/temp/2.jpg" /></a>
<a href="#" data-id="11"><img src="img/temp/3.jpg" /></a>
<a href="#" data-id="12"><img src="img/temp/4.jpg" /></a>
<a href="#" data-id="13"><img src="img/temp/5.jpg" /></a>
<a href="#" data-id="14"><img src="img/temp/6.jpg" /></a>
<a href="#" data-id="15"><img src="img/temp/7.jpg" /></a>
<a href="#" data-id="16"><img src="img/temp/8.jpg" /></a>
</li>
I tried with below code, but stuck where to end while and start again. 我尝试使用下面的代码,但停留在何处结束并重新开始。
<<li>
<?php
$counter = 1;
while( have_rows('our_clients') ):the_row();
$image = get_sub_field('image');
?>
<a href="javascript:void(0);" data-id="<?php echo $counter;?>"><img src="<?php echo $image;?>" /></a>
<?php $counter++; endwhile; ?>
</li>>
You can check $counter
inside while loop with %
Modulus operator for 16
, if $counter % 16 == 0
that means your loop is done 16 times, therefore close and open li tags, and do the same for all multiple of 16. 您可以使用
%
Modulus运算符检查while循环内的$counter
是否为16
,如果$counter % 16 == 0
意味着您的循环完成了16次,因此请关闭和打开li标签,并对16的所有倍数执行相同的操作。
<li>
<?php
$counter = 0;
while( have_rows('our_clients') ):the_row();
if($counter > 0 && $counter % 16 == 0)
echo '</li><li>';
$image = get_sub_field('image');
?>
<a href="javascript:void(0);" data-id="<?php echo $counter;?>"><img src="<?php echo $image;?>" /></a>
<?php $counter++; endwhile; ?>
</li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.