1

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 :

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

1

1 Answer 1

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 )
Sign up to request clarification or add additional context in comments.

3 Comments

Wicked thanks a lot ! That did the trick ! But I have one little problem left : the class "last" is added to every 1rst post instead of every 3rd post :/ ... do you know why ?
You need to updated your question with actual/updated code than i will be able to check that
Ok, thanks to Ravi Patel who actually fixed my issue, it was a syntax problem, and my counter was starting at 0 instead of 1.. The corrected code above is up and runnig, hurray !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.