Php counter in WordPress loop

I would like to add special class “last” to every third post of my loop to get my columns right (I have 3 vertical columns).

I have found this bit of code on another post :

Read More
<?php $loop = new WP_Query( array( 'post_type' => 'portfolio' ) ); ?>
    <?php
        $i = 1; //first value of $i
        while ( $loop->have_posts() ) : $loop->the_post(); 
           if( $i % 3 == 0 ): // for every three post
             $class = 'last';
           else:
             $class = ''; 
           endif;

        ?>
    <div class="four columns <?php echo $class ?>">
        <?php the_content(); //along with other stuff in looped div ?
           $i++;
         >
    </div>
<?php endwhile ?>

It seems to be the right option for me but my problem is that my articles are displayed this way :

 <article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' );  ?>>

That means I cannot add the echo $class to my lopped article, and I cannot find the correct syntax to get it right..!

Anybody ? Thanks !

Related posts

Leave a Reply

1 comment

  1. using above case try to change

    <article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' );  ?>>
    

    to

    <article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post '. $class );  ?>>
    

    change

    if( $i % 4 == 0 ) 
    

    to

    if( $i % 3 == 0 )