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.

0

2 Answers 2

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

this line is wrong, use either

<?php $counter++; ?>

or

<?php $counter = $counter +1; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

..or $counter += 1;
Perfect. Thank you!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.