Title: Controlling how the loop repeats elements
Last modified: August 19, 2016

---

# Controlling how the loop repeats elements

 *  Resolved [matphoto](https://wordpress.org/support/users/matphoto/)
 * (@matphoto)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/controlling-how-the-loop-repeats-elements/)
 * I set up a small 3-post loop on my static page to show the excerpts of featured
   posts. It’s working alright, but I want to separate them like so:
 * post | post | post
 * with a line between them. However, since the loop repeats all elements, it ends
   up like this:
 * post | post | post |
 * Is there a way to stop the last element from appearing like that? On a similar
   note, I’d also like to know how to alternate divs for each post in the loop, 
   like this:
 *     ```
       begin loop
       <div class="1">Post</div>
       <div class="2">Post</div>
       <div class="1">Post</div>
       <div class="2">Post</div>
       end loop
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/controlling-how-the-loop-repeats-elements/#post-1389899)
 * one of the ways is to use a counter variable;
    have you dividing border on the
   left side of the posts, and not show it for the first post: (this is not a copy/
   paste instruction)
 *     ```
       <?php $counter=0;
       ...query_posts...if you need it...
       if (have_posts()) :
       while (have_posts()) : the_post(); ?>
       <div class="main_post_wrap" <?php if($counter==0) { echo ' style="border-left:none;" ?>>
       ...post title, content, postmetadata etc. ...
       </div> <!--end of .main_post_wrap -->
       <?php $counter++; ?>
       <?php endwhile; endif; ?>
       ```
   
 * similar method for your second question;
    using the modulus operator (a%2 – remainder
   of a division) with $counter:
 *     ```
       <?php $counter=0;
       ...query_posts...if you need it...
       if (have_posts()) :
       while (have_posts()) : the_post(); ?>
       <div <?php echo 'class="'.($counter%2+1).'"'; ?>>
       ...post title, content, postmetadata etc. ...
       </div> <!--end of .1 or .2 -->
       <?php $counter++; ?>
       <?php endwhile; endif; ?>
       ```
   
 *  Thread Starter [matphoto](https://wordpress.org/support/users/matphoto/)
 * (@matphoto)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/controlling-how-the-loop-repeats-elements/#post-1389935)
 * awesome, thanks I’ll try it tonight
 *  Thread Starter [matphoto](https://wordpress.org/support/users/matphoto/)
 * (@matphoto)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/controlling-how-the-loop-repeats-elements/#post-1390038)
 * worked perfectly, thanks a lot!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Controlling how the loop repeats elements’ is closed to new replies.

## Tags

 * [the-loop](https://wordpress.org/support/topic-tag/the-loop/)

 * 3 replies
 * 2 participants
 * Last reply from: [matphoto](https://wordpress.org/support/users/matphoto/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/controlling-how-the-loop-repeats-elements/#post-1390038)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
