Title: WP_Query items count
Last modified: August 21, 2016

---

# WP_Query items count

 *  Resolved [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/wp_query-items-count/)
 * Hello, i have this code [http://pastebin.com/9Jgwihr7](http://pastebin.com/9Jgwihr7)
 * Now this code loop first 6 posts without starting <div class=”row”><div class
   =”movie_list”>
 * and it’s breaks my template, maybe anybody knows how to correctly dot his counting?
 * Thanks.

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/wp_query-items-count/#post-4395154)
 * your code is using php shorttags `<?` which are not understood by all servers;
 * try with:
 *     ```
       <?php  if($i % 6 == 0) {echo '</div></div><div class="row"><div class="movie_list">'; ?>
                                        <?php } $i++; ?>
       ```
   
 *  Thread Starter [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/wp_query-items-count/#post-4395280)
 * Nothing changes.
 * Now it loop like this:
 *     ```
       <div class="container">
       <div class="col-md-2"></div>
       <div class="col-md-2"></div>
       <div class="col-md-2"></div>
       <div class="col-md-2"></div>
       <div class="col-md-2"></div>
       <div class="col-md-2"></div>
       </div>
       <div class="row"></div>
       <div class="row"></div>
       <div class="row"></div>
       ```
   
 * It’s should be like this:
 *     ```
       <div class="container">
       <div class="row"></div>
       <div class="row"></div>
       <div class="row"></div>
       <div class="row"></div>
       </div>
       ```
   
 * The 6 posts inside div row, now first 6 posts dont have div “row”, and last 3
   div “row” is out off div “container”.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/wp_query-items-count/#post-4395290)
 * I cannot reproduce you problem –
    I ran your code in my test site (local install)
   and the html structure shows as it should be; there is just one empty div too
   much at the end.
 * can you post a live link to a page with that template?
 * alternative structure (no fixed open and close divs, all done by conditional 
   statements using the lop counter `current_post`)
 *     ```
       <?php $custom_query = new WP_Query( $args );
       while($custom_query->have_posts()) : $custom_query->the_post();
       				   if($custom_query->current_post % 6 == 0) { echo "\n".'<div class="row"><div class="movie_list">'; } //open divs ?>
       				 <div class="col-md-2">
       					<div class="well">
       						OUTPUT
       					</div>
       				 </div>
       				 <?php if($custom_query->current_post % 6 == 5 || $custom_query->current_post == $custom_query->post_count-1 ) { echo "\n".'</div></div>'; } //closing divs
       endwhile;
       wp_reset_postdata(); // reset the query ?>
       ```
   
 *  Thread Starter [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/wp_query-items-count/#post-4395292)
 * Here is the full file code:
    [http://pastebin.com/SdA58VRV](http://pastebin.com/SdA58VRV)
 * Here is the live demo of this code:
    [http://test.filmas24.lt/top-filmai/](http://test.filmas24.lt/top-filmai/)
 * With your’s code i get this error:
    `Parse error: syntax error, unexpected $end
   in /home/patiekal/domains/filmas24.lt/public_html/test/wp-content/themes/filmai_theme/
   page-templates/page-top-movies.php on line 1`
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/wp_query-items-count/#post-4395295)
 * to eliminate any pph problems, try to change this section:
 *     ```
       <?php
   
         //Set the counter to 1
         $i = 1;
   
         //Open the row div
         echo '<div class="row"><div class="movie_list">'; ?>
       ```
   
 * to:
 *     ```
       <?php
   
         //Set the counter to 1
         $i = 1; ?>
   
       <div class="row"><div class="movie_list">
       ```
   
 * > With your’s code i get this error:
 * the code is not tested and is not meant for copy/paste – just as an illustration
   of the use of ‘current_post’ to output the divs.
 *  Thread Starter [modeman](https://wordpress.org/support/users/modeman/)
 * (@modeman)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/wp_query-items-count/#post-4395296)
 * Worked, big thanks to you 🙂

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

The topic ‘WP_Query items count’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [modeman](https://wordpress.org/support/users/modeman/)
 * Last activity: [12 years, 5 months ago](https://wordpress.org/support/topic/wp_query-items-count/#post-4395296)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
