• Resolved apratherjr

    (@apratherjr)


    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)
  • $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

    (@apratherjr)

    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; ?>
    <?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

    Thread Starter apratherjr

    (@apratherjr)

    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

    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

    (@apratherjr)

    Alright… duly noted… Learned a lot, Thanks for the rich information.

    My guess is that i was originally chosen because used as increment.

    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.