在while循环中实现PHP计数器以使用ul元素包装列表项

时间:2016-11-04 13:48:23

标签: php

我在实现PHP计数器时遇到困难,将列表元素包装在无序列表标记中。

我想要回滚每3个列表元素。

我一直在尝试使用这些先前的问题作为指导,但收效甚微。

easier way to get counter from while loop?

php loop counter bootstrap row

            <?php

            $counter = 0; 

            echo '<ul class="products latestCourses">';

            while ( have_posts() ) : the_post(); ?>

                <?php wc_get_template_part( 'content', 'product' ); ?>

            <?php $counter = $counter++; ?>

            <?php if ($counter%3 == 0) echo '</ul><ul class="products latestCourses">'; ?>

            <?php endwhile; // end of the loop. ?>

这就是我到目前为止页面模板只包含一个列表元素。

目前,此代码将每个列表项包装在一个无序列表中。

2 个答案:

答案 0 :(得分:4)

<?php $counter = $counter++; ?>

此行错误,请使用

<?php $counter++; ?>

<?php $counter = $counter +1; ?>

答案 1 :(得分:1)

使用此增量1增加 $counter = $counter + 1;


$counter = $counter + n;
在哪里&#39; n&#39;是您想要增加的所需数字