counter trickery – adding an extra bit from outside the loop but in sequence
-
This is the current code. The counter goes up to 3 then resets to 1, and the article is given the appropriate class. Works fabulously.
<?php $counter = 0; ?> <?php if(have_posts()) : while(have_posts()) : the_post(); <?php $counter++; ?> <article class="th<?php echo $counter; ?>"> <?php // reset counter if 3 has been reached if ($counter==3){ $counter=0; } ?> <!--stuff--> </article> <?php } endwhile; endif; ?> <article class="th1"> <!--stuff for an extra article--> </article>
Now I have to pull in one extra article. it isn’t part of the loop because it’s from a different area. Right now it has a class of 1 all the time but what should happen is that it should flow with the rest. It doesn’t matter if it’s first or last. So I think the problem is either:
– the first loop starts with 2, goes to 3, then goes back to 1, 2, 3, until the posts are finished.
OR
– loop goes 1, 2, 3 until all posts are finished. Then figure what was the last number, and add one to this article outside the loop.
Is either of these possible? Is there an easier way?
- The topic ‘counter trickery – adding an extra bit from outside the loop but in sequence’ is closed to new replies.