Title: Post loop Codes&#8230; Need Help
Last modified: August 20, 2016

---

# Post loop Codes… Need Help

 *  Resolved [apratherjr](https://wordpress.org/support/users/apratherjr/)
 * (@apratherjr)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/)
 * Most frequent of time looking at several free theme codes. I have frequently 
   notice this code to begin with…
 *     ```
       <?php get_header(); ?>
   
       <?php if (have_posts()) : ?>
   
       <?php $i = 0; ?>
       <?php while (have_posts()) : the_post(); $i++; ?>
       ```
   
 * Its easy to comprehend that part. But for bold one…
 * **<?php $i = 0; ?>** and **$i++;**
 * What is this codes? what is it used for? i tried to look for one of these in 
   codex, and I am not having luck. Can anyone explain? or point out information?

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/#post-2941790)
 * `$i` is simply a variable which gets incrased for each post in the loop;
 * might be used for various purposes, such as styling some posts different, or 
   for inserting someting after a certain number of posts ……
 * if you can post a full example code, you might get more information.
 *  Thread Starter [apratherjr](https://wordpress.org/support/users/apratherjr/)
 * (@apratherjr)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/#post-2941807)
 * first, thank you for replying… I think I am getting picture.. but not entirely.
   Let have you look example code below, hope that helps explaining completely.
 *     ```
       <?php if ( have_posts() ) : ?>
           <?php $i = 0; ?>
           <?php while ( have_posts() ) : the_post(); $i++; ?>
   
           <div class="portf">
   
       <a href="<?php the_permalink() ?>" class="thumb" title="<?php the_title(); ?>">
   
                   <?php if (has_post_thumbnail()) : ?>
   
                       <?php the_post_thumbnail(array(150,150)); ?>
   
                   <?php else : ?>
   
                       <img src="<?php bloginfo('template_url'); ?>/img/logoA.jpg" width="150" height="150" alt=""/>
   
                   <?php endif; ?>
   
        </a><!-- permalink to thumbnail -->
   
               <div><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
   
           </div><!-- .portf -->
   
           <?php if ($i % 6 == 0) echo '<div style="clear: both;"></div>'?>
   
           <?php endwhile; ?>
   
       <?php endif; ?>
       ```
   
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/#post-2941810)
 *     ```
       <?php if ($i % 6 == 0) echo '<div style="clear: both;"></div>'?>
       ```
   
 * this reads: after every sixth post, add a div to clear floats (for formatting,
   possibly columns or grid layout)
 * this `$i % 6` is the modulus operator which basically returns the remainder of
   the division; i.e. if $i is a multiple of 6, this will return 0 zero.
 * [http://php.net/manual/en/language.operators.arithmetic.php](http://php.net/manual/en/language.operators.arithmetic.php)
 *  Thread Starter [apratherjr](https://wordpress.org/support/users/apratherjr/)
 * (@apratherjr)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/#post-2941824)
 * Awesome… thank you for clarifying this up including link.
 * but why %i? why not $a or other letter, Or is it the standard protocol?
 * again, thank for everythin
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/#post-2941827)
 * > but why $i? why not $a or other letter
 * any valid php variable name would have done the job;
 * **$i** seems to be a ‘standard = first choice’ variable name for loops, possibly
   from the early days of BASIC programming …
 * also in math generally, **i**, **j**, **k** etc are commonly used as integer 
   variables in any kind of loops; as compared to **x**, **y**, **z** used for floating
   point number variables.
 *  Thread Starter [apratherjr](https://wordpress.org/support/users/apratherjr/)
 * (@apratherjr)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/#post-2941831)
 * Alright… duly noted… Learned a lot, Thanks for the rich information.
 *  [vhf](https://wordpress.org/support/users/kortchnoi/)
 * (@kortchnoi)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/#post-2941845)
 * My guess is that **i** was originally chosen because used as **i**ncrement.
 * **j, k** followed alphabetically.

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

The topic ‘Post loop Codes… Need Help’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 7 replies
 * 3 participants
 * Last reply from: [vhf](https://wordpress.org/support/users/kortchnoi/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/post-loop-codes-need-help/#post-2941845)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
