Title: need custom loop code
Last modified: August 21, 2016

---

# need custom loop code

 *  Resolved [cipriano200](https://wordpress.org/support/users/cipriano200/)
 * (@cipriano200)
 * [12 years ago](https://wordpress.org/support/topic/need-custom-loop-code/)
 * Hello lads
 * Anyone knows how to run this kind of loop.
 * [http://s23.postimg.org/4n55gvpt7/Capture.jpg](http://s23.postimg.org/4n55gvpt7/Capture.jpg)
 * Basicly the loop after runs 4 times needs to close li tag then start another 
   li with the divs like in the image
    I have a code to loop, but does not work 
   like i want.
 * THANK YOU

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years ago](https://wordpress.org/support/topic/need-custom-loop-code/#post-4901468)
 * you can use the build-in lop counter to determine the output of the li tags;
   
   example:
 *     ```
       <?php while( have_posts() ) : the_post();
       if( $wp_query->current_post%4 == 0 ) echo '<li>'; ?>
       <div>
       <?php echo 'post'.($wp_query->current_post+1); //output here// ?>
       </div>
       <?php if( $wp_query->current_post%4 == 3 || $wp_query->current_post == $wp_query->post_count-1 ) echo '</li>';
       endwhile; ?>
       ```
   
 *  Thread Starter [cipriano200](https://wordpress.org/support/users/cipriano200/)
 * (@cipriano200)
 * [12 years ago](https://wordpress.org/support/topic/need-custom-loop-code/#post-4901475)
 * worked like a charm
 * Great help and thank you.
 *  Thread Starter [cipriano200](https://wordpress.org/support/users/cipriano200/)
 * (@cipriano200)
 * [12 years ago](https://wordpress.org/support/topic/need-custom-loop-code/#post-4901480)
 * How to implement into this code with li tags?
 * That code works for posts
 *     ```
       <?php /* Time to create a new loop that pulls out image attachments for this post */
   
       $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'numberposts' => 999, 'post_status' => null, 'post_parent' => $post->ID, 'orderby' => 'menu_order ID', 'order' => 'ASC', );
       $attachments = get_posts( $args );
       if ( $attachments ) {
       foreach ( $attachments as $attachment ) {
       $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'full' );
   
       echo '<div class="centerImg">';
       echo '<a rel="prettyPhoto-img" href="';
       echo $image_attributes[0];
       echo '">';
       echo '<img src="';
       echo $image_attributes[0];
       echo '" />';
       echo '</div>';
   
       ?>
       <?php } ?>
       <?php } ?>
       ```
   
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years ago](https://wordpress.org/support/topic/need-custom-loop-code/#post-4901487)
 * you need to run a counter variable in the foreach loop, and check for _‘before
   position 1,5,9,etc’_ and _‘after position 4,8,12,etc,or after last in loop’_ –
   a pure php exercise and nothing particular to WordPress.
 * example:
 *     ```
       <?php /* Time to create a new loop that pulls out image attachments for this post */
   
       $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'numberposts' => 999, 'post_status' => null, 'post_parent' => $post->ID, 'orderby' => 'menu_order ID', 'order' => 'ASC', );
       $attachments = get_posts( $args );
       if ( $attachments ) {
       	$counter = 0;
       	echo '<ul>';
       	foreach ( $attachments as $attachment ) {
       		$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'full' );
   
       		if( $counter%4 == 0 ) echo '<li>'; //open <li> before every fourth item
   
       		echo '<div class="centerImg">';
       		echo '<a rel="prettyPhoto-img" href="';
       		echo $image_attributes[0];
       		echo '">';
       		echo '<img src="';
       		echo $image_attributes[0];
       		echo '" />';
       		echo '</a>';
       		echo '</div>';
   
       		if( $counter%4 == 3 || $counter == count( $attachments )-1 ) echo '</li>'; //close </li> after each block of four or after last item
   
       		$counter++; ?>
       	<?php }
       	echo '</ul>'; ?>
   
       <?php } ?>
       ```
   
 *  Thread Starter [cipriano200](https://wordpress.org/support/users/cipriano200/)
 * (@cipriano200)
 * [12 years ago](https://wordpress.org/support/topic/need-custom-loop-code/#post-4901491)
 * WORKED!
 * THANK YOU AGAIN
 *  [Muhammad Zohaib](https://wordpress.org/support/users/fangsword/)
 * (@fangsword)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/need-custom-loop-code/#post-4901778)
 * Thank you so much! that helped me too 🙂

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

The topic ‘need custom loop code’ is closed to new replies.

 * 6 replies
 * 3 participants
 * Last reply from: [Muhammad Zohaib](https://wordpress.org/support/users/fangsword/)
 * Last activity: [11 years, 4 months ago](https://wordpress.org/support/topic/need-custom-loop-code/#post-4901778)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
