2016-11-04 38 views
0

我很难实现一个PHP计数器来将我的列表元素包裹在无序列表标记中。在while循环内部实现一个PHP计数器来包装带有ul元素的列表项目

我想环绕每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. ?> 

这是我迄今为止页面模板只包含一个列表元素。

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

回答

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

此行是错误的,使用所需的数要么

<?php $counter++; ?> 

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

..或者'$计数器+ = 1;' – simon

+0

完善。谢谢! –

1

用这个增量由1
$counter = $counter + 1;


$counter = $counter + n;
其中 'n' 是要增加