Title: loops inside the loop
Last modified: August 19, 2016

---

# loops inside the loop

 *  [baalinato](https://wordpress.org/support/users/baalinato/)
 * (@baalinato)
 * [18 years ago](https://wordpress.org/support/topic/loops-inside-the-loop/)
 * i want my loop to display 4 posts, then echo </div><div class=small4″>
    until
   there is no more posts. for now, it works only one time.
 * my code:
 *     ```
       <div class="small4"> 
   
       <?php $split = ceil( count($posts) / 4 ); ?>
   
       <?php if (is_category('photos1')) {query_posts ('category_name=photos1');} ?>
   
       <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
   
          <div class="post" id="post-<?php the_ID(); ?>">
            <div class="entry">
              <?php the_content(''); ?>
            </div>
          </div>
          <div class="spacer"></div>
   
       <?php if( $post == $posts[$split] ) :
        echo "</div><div class=\"small4\">" ?>
   
       <?php  endif; ?>	
   
       <?php endwhile; endif; ?>
   
       </div>
       ```
   

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

 *  Thread Starter [baalinato](https://wordpress.org/support/users/baalinato/)
 * (@baalinato)
 * [18 years ago](https://wordpress.org/support/topic/loops-inside-the-loop/#post-773469)
 * I found the answer
 *     ```
       <div class="small4"> 
   
       <?php if (is_category('photos1')) {query_posts ('category_name=photos1');} ?>
   
       <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
   
       			<div class="post" id="post-<?php the_ID(); ?>">
   
       				<div class="entry">
       					<?php the_content(''); ?>
   
       					<?php edit_post_link('Edit', '<p>', '</p>'); ?>
       				</div>
   
       			</div>
       		<div class="spaceit"></div>
   
       <strong><?php
       $count++;
       $y = 4;
       $r = fmod($count, $y);
       if ($r==0)
       {
       echo '</div><div class="small4">';
       }
       ?></strong>
   
       		 <?php endwhile; endif; ?>
   
       		</div>
       ```
   
 *  [binh](https://wordpress.org/support/users/binh/)
 * (@binh)
 * [18 years ago](https://wordpress.org/support/topic/loops-inside-the-loop/#post-773646)
 * I’m glad you made it yourself. Do you mind share why do you wanna do so? Any 
   demo?
 * Regards,
    [sig moderated. Resist the urge please]
 *  [binh](https://wordpress.org/support/users/binh/)
 * (@binh)
 * [18 years ago](https://wordpress.org/support/topic/loops-inside-the-loop/#post-773648)
 * Another way to loop is like this:
 *     ```
       $count++;
       if ($count==3)
       {
       $count=0;
       echo '</div><div class="small4">';
       ```
   
 * What does it mean? If count is 3, that mean every 4th times it will reset to 
   0 and count again.
 * 🙂 maybe it will be faster than doing fmod function 😉
 *  Thread Starter [baalinato](https://wordpress.org/support/users/baalinato/)
 * (@baalinato)
 * [18 years ago](https://wordpress.org/support/topic/loops-inside-the-loop/#post-773681)
 * i tried urs and i had to change the 3 for a 4 to make it work. seems longuer.
   maybe an illusion.
 * here’s a demo : [http://baa-li.com/test/photos1/](http://baa-li.com/test/photos1/)
 * the code on the template category :
 *     ```
       <?php get_header(); ?>
   
       <!--  photo1  cat  -->
   
       	<div id="content" class="catphotos">
   
       	<?php $my_query = new WP_Query('category_name=photos1&p=4'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
   
       		<div class="small1">
   
       			<h2>Photos 1</h2>
   
       			<div class="spaceit"></div>
   
       			<div id="post-<?php the_ID(); ?>">
   
       				<div class="entry">
       					<?php the_content(''); ?>
   
       					<?php edit_post_link('Edit', '<p>', '</p>'); ?>
       				</div>
   
       			</div>
       		</div>
   
       		<?php endwhile; ?>
   
        		<div class="small4"> 
   
       <?php if (is_category('photos1')) {query_posts ('category_name=photos1');} ?>
   
       <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
   
       			<div class="post" id="post-<?php the_ID(); ?>">
   
       				<div class="entry">
       					<?php the_content(''); ?>
   
       					<?php edit_post_link('Edit', '<p>', '</p>'); ?>
       				</div>
   
       			</div>
       		<div class="spaceit"></div>
   
       <?php
       $count++;
       $y = 4;
       $r = fmod($count, $y);
       if ($r==0)
       {
       echo '</div><div class="small4">';
       }
       ?>
   
       		 <?php endwhile; endif; ?>
   
       		</div>
   
       	</div>
   
       <?php get_sidebar(); ?>
   
       <?php get_footer(); ?>
       ```
   
 *  Thread Starter [baalinato](https://wordpress.org/support/users/baalinato/)
 * (@baalinato)
 * [18 years ago](https://wordpress.org/support/topic/loops-inside-the-loop/#post-773685)
 * thanks a lot binh for ur interest.
    hi from montreal to vietnam.

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

The topic ‘loops inside the loop’ is closed to new replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 2 participants
 * Last reply from: [baalinato](https://wordpress.org/support/users/baalinato/)
 * Last activity: [18 years ago](https://wordpress.org/support/topic/loops-inside-the-loop/#post-773685)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
