PHP counter increment for every 1st and 4th div

I am running a wordpress if while statement and would like my items to display in a column format.

I am currently using 960.gs which is your standard 960 grid system, and by default adds 10px padding to left and right, by simply passing a class of alpha or omega you can get rid of these.

Read More

How do I get php to execute a statment for every 1st one to add alpha and 4th one omega?

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="grid_4">
        <?php $wp_query->is_home = false; ?>
        <div class="entry">
            <h3 style="margin-bottom:10px;"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
             <?php //the_excerpt() ?>
         </div>
     </div>
<?php endwhile; endif; ?>

Related posts

Leave a Reply

3 comments

  1. Try maybe something like this. I wasn’t sure what you meant by every 1st and 4th so I made it the way you can see. You should be able to customize it yourself.

    <?php $counter = 0; ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php
            $div = '<div class="entry';
            if ($counter % 4 == 0)
                $div .= " alpha";
            else if($counter % 4 == 3)
                $div .= " omega";
            echo $div . '">';
        ?>
        <div class="grid_4">
            <?php $wp_query->is_home = false; ?>
            <div class="entry">
                <h3 style="margin-bottom:10px;"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                 <?php //the_excerpt() ?>
             </div>
         </div>
         <?php $counter++; ?>
    <?php endwhile; endif; ?>
    
  2. Try this:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <div class="grid_4">
            <?php $wp_query->is_home = false; ?>
            <div class="entry <?php if (!($i++ % 4)) { ?>alpha<?php } ?> <?php if (!(($j++)+1 % 4)) { ?>omega<?php } ?>">
                <h3 style="margin-bottom:10px;"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                <?php //the_excerpt() ?>
            </div>
        </div>
    <?php endwhile; endif; ?>
    
  3. Looks kind of like a repeat of something I’ve seen recently. (Not your fault, it was poorly named and you wouldn’t have been able to search on it). Help needed improving a foreach loop

    I think what you’re looking for would be more precisely described as class=alpha on row 4k+1, class=omega on row=4k

    I’m not familiar with your if/while syntax, and the embedded PHP throws me off a little. But I think what you want to do:

    is initialize a counter to 0 before the while loop ($i = 0). Increment it at the end of the loop ($i++)

    I’m not sure where you want to put these classes, but put if ($i%4==1) class=”alpha” and if(i%4==0) class=”omega” (add php tags as appropriate, for some reason they werent showing up right for me).