I am trying to do the following:

1) assign col-lg-4 class to the first element in the loop, when there are 3 or more posts within the given category.

2) assign col-lg-8 class to the first element in the loop, when there are 2 posts within the given category.

3) leave the col-sm-12 class on the first element in the loop, when there is only one post within the given category.

And so, here's what I thought of:

<div class="container special-offer-wrap">
    <div class="row special-offer-wrap special-offer-row">
            $columns_query_args = array(
                'category_name' => 'featured',
                'posts_per_page' => '3'
        <?php $columns_query = new WP_Query( $columns_query_args ); ?>
        <?php if ( $columns_query->have_posts() ) : ?>
            <?php while ( $columns_query->have_posts() ) : $columns_query->the_post(); ?>
                <div class="col-sm-12
                    <?php if( $columns_query_item_number == 0){
                        if( $the_query->found_posts == 1 ){
                        echo ' ';
                        }  else {
                            if( $the_query->found_posts == 2 ){
                            echo 'col-lg-8';
                            }  else {
                            echo 'col-lg-4';
                        } else echo 'col-lg-4'; /*all other posts */
                        $columns_query_item_number++;  ?>">
                    <div class="special-offer-description">
                        <h2><?php the_title(); ?></h2>
                        <?php the_excerpt( ); ?>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        <?php else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.', 'fresh_spring' ); ?></p>
        <?php endif; ?>

It looks like this, when there are 3 posts within the category:

The problem is that it doesn't change to the one similar to this:

when I come back to 2 posts within the category (by removing the category from the 3rd post), but instead it keeps the col-lg-4 class on the first element.

Any ideas? I guess I messed something up with the query conditions.


On the suggestion by @Michael, I used debugging mode which showed me some problems with defining the variable $columns_query_item_number. Now it is defined properly and the only notice I get is the one about woocommerce_get_page_id being deprecated (although I don't have it right on my site, only in debug.log file, so I don't bother yet).

The other thing I did is that I changed $the_query to $columns_query, which right away got me to the solution, but the problem now is that the next loop has been compromised somehow. I don't really know why because the loop is reset at the end by wp_reset_postdata() function.

  • simple mistake: your loop is based on $columns_query while you are using $the_query in your conditional ... to get warnings and error messages, while developing your site, try and enable DEBUG codex.wordpress.org/Debugging_in_WordPress
    – Michael
    Commented Jun 25, 2017 at 1:22
  • I followed your tips and some problems still hold, see edit. Commented Jun 25, 2017 at 10:44

1 Answer 1


If wp_reset_postdata() doesn't work as expected you can backup the original $post object by inserting

global $post; // if you aren't in global scope

before doing your WP_Query and restore it with


right after wp_reset_postdata();

  • Now the code there looks like this: <?php global $post; $postBk=$post; $columns_query_item_number = 0; ?> ... <?php wp_reset_postdata(); ?> <?php $post=$postBk; ?> And it doesn't work still... Commented Jun 25, 2017 at 19:11
  • Now it works, although I don't see difference in code. <?php global $post; $postBk=$post; $columns_query_item_number = 0; $columns_query_args = array( 'category_name' => 'featured', 'posts_per_page' => '3' ) $columns_query = new WP_Query( $columns_query_args ); if ( $columns_query->have_posts() ) : ?> SOME CODE HERE <?php wp_reset_postdata(); ?> <?php else : ?> <p>code here</p> <?php endif; ?> <a href=""></a> <?php $post=$postBk; ?> Commented Aug 16, 2017 at 16:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.