0

I am having difficulty implementing a PHP counter, to wrap my list elements in unordered list tags.

I want to wrap around every 3 list elements.

I have been trying to use these previous questions as a guide but to little avail.

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. ?>

This is what I have so far the page template simply contains a list element.

Currently this code is wrapping every list item in an unorder list.

Community
  • 1
  • 1
Brett Golding
  • 85
  • 1
  • 11

2 Answers2

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

this line is wrong, use either

<?php $counter++; ?>

or

<?php $counter = $counter +1; ?>
Yannis Berrouag
  • 356
  • 2
  • 8
1

Use this for increment by 1
$counter = $counter + 1;

or
$counter = $counter + n;
where 'n' is the desired number by which you want to increment

Aakash Martand
  • 926
  • 1
  • 8
  • 21