Title: Count posts in query
Last modified: August 19, 2016

---

# Count posts in query

 *  [Darfuria](https://wordpress.org/support/users/darfuria/)
 * (@darfuria)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/count-posts-in-query/)
 * I’m running a query that’s selecting a bunch of posts. I’d like to be able to
   put a class in the containing div for each post that has an integer that increases
   with each post in that loop.
 * So essentially the classes would be post1, post2, post3, post4, etc. and I could
   therefore target them with a stylesheet.
 * Any thoughts?
 * Thanks.

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

 *  [tugbucket](https://wordpress.org/support/users/tugbucket/)
 * (@tugbucket)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/count-posts-in-query/#post-1242692)
 * Sure. In and around your loop add a few lines:
 *     ```
       <?php if (have_posts()) : ?>
       <?php $i = 0; ?>
       <?php while (have_posts()) : the_post(); ?>
       <?php $i++; ?>
       <div id="post-<?php the_ID(); ?>" class="post<?php echo $i; ?>">
       ```
   
 * That will make your posts have the classes of:
 * post1
    post2 post3 etc…
 *  Thread Starter [Darfuria](https://wordpress.org/support/users/darfuria/)
 * (@darfuria)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/count-posts-in-query/#post-1242700)
 * Ah, clever!
 * Thanks

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

The topic ‘Count posts in query’ is closed to new replies.

 * 2 replies
 * 2 participants
 * Last reply from: [Darfuria](https://wordpress.org/support/users/darfuria/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/count-posts-in-query/#post-1242700)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
