Title: Conditional Loop: What&#039;s wrong?
Last modified: August 21, 2016

---

# Conditional Loop: What's wrong?

 *  [n00bie12](https://wordpress.org/support/users/n00bie12/)
 * (@n00bie12)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/conditional-loop-whats-wrong/)
 * Trying to use conditional statements in loop to apply different bootstrap classes
   to different posts. What am I doing wrong, here? Thx, in advance, for your help.
 *     ```
       <?php while ( have_posts() ) : the_post(); ?>
   
       <?php if ( $wp_query->
       current_post == 1 ) && !is_paged() : ?>
   
       <div class="container">
   
         <div class="row">
           <div class="span12">
             <h1>
               <?php the_title(); ?>
             </h1>
             <p>
               <?php the_excerpt(); ?>
             </p>
   
           </div>
           <!-- .span12 -->
         </div>
        <div class="row">
   
           <?php else if ( $wp_query->
       current_post == 2 || $wp_query->
       current_post == 3 || $wp_query->
       current_post == 4 && !is_paged() ) : ?>
   
          <div class="span4">
            <h2>
              <?php the_title(); ?>
            </h2>
            <p>
              <?php the_excerpt(); ?>
            </p>
          </div>
          <!-- .span4 -->
   
          <?php else if ( $wp_query->
       current_post == 5 && !is_paged() ) : ?>
          </div>
          <!-- .row -->
   
          <div class="row">
            <div class="span3">
              <h2>
                <?php the_title(); ?>
              </h2>
              <p>
                <?php the_excerpt(); ?>
              </p>
            </div>
            <!-- .span3 -->
   
            <?php else if ( $wp_query->
       current_post == 6 || $wp_query->
       current_post == 7 || $wp_query->
       current_post == 8 && !is_paged() ) : ?>
   
          <div class="span3">
            <h2>
              <?php the_title(); ?>
            </h2>
            <p>
              <?php the_excerpt(); ?>
            </p>
          </div>
          <!-- .span3 -->
   
          <?php else if ( $wp_query->
       current_post == 8 && !is_paged() ) : ?>
   
          <div class="span3">
            <h2>
              <?php the_title(); ?>
            </h2>
            <p>
              <?php the_excerpt(); ?>
            </p>
          </div>
          <!-- .span3 -->
   
          </div>
          <!-- .row -->
       </div>
       <!-- .container -->
   
       <?php else : ?>
   
       <p>
         Error.
       </p>
   
       <?php endif; ?>
   
       <?php endwhile; ?>
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [n00bie12](https://wordpress.org/support/users/n00bie12/)
 * (@n00bie12)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/conditional-loop-whats-wrong/#post-3930336)
 * I tried to find/fix some mistakes, but still not working. Please advise:
 *     ```
       <?php while ( have_posts() ) : the_post(); ?>
   
       <?php if ( $wp_query->
       current_post == 1 && !is_paged() ) { ?>
   
       <div class="container">
   
         <div class="row">
           <div class="span12">
             <h1>
               <?php the_title(); ?>
             </h1>
             <p>
               <?php the_excerpt(); ?>
             </p>
   
           </div>
           <!-- .span12 -->
         </div>
   
         <div class="row">
           <?php } ?>
   
           <?php elseif ( $wp_query->
       current_post == 2 || $wp_query->
       current_post == 3 || $wp_query->
       current_post == 4 && !is_paged() ) { ?>
   
          <div class="span4">
            <h2>
              <?php the_title(); ?>
            </h2>
            <p>
              <?php the_excerpt(); ?>
            </p>
          </div>
          <!-- .span4 -->
   
          <?php } ?>
   
          <?php elseif ( $wp_query->
       current_post == 5 && !is_paged() ) { ?>
   
          </div>
          <!-- .row -->
   
          <div class="row">
            <div class="span3">
              <h2>
                <?php the_title(); ?>
              </h2>
              <p>
                <?php the_excerpt(); ?>
              </p>
            </div>
            <!-- .span3 -->
   
            <?php } ?>
   
            <?php elseif ( $wp_query->
       current_post == 6 || $wp_query->
       current_post == 7 || $wp_query->
       current_post == 8 && !is_paged() ) { ?>
   
          <div class="span3">
            <h2>
              <?php the_title(); ?>
            </h2>
            <p>
              <?php the_excerpt(); ?>
            </p>
          </div>
          <!-- .span3 -->
   
          <?php } ?>
   
          <?php elseif ( $wp_query->
       current_post == 8 && !is_paged() ) { ?>
   
          <div class="span3">
            <h2>
              <?php the_title(); ?>
            </h2>
            <p>
              <?php the_excerpt(); ?>
            </p>
          </div>
          <!-- .span3 -->
   
          </div>
          <!-- .row -->
       </div>
       <!-- .container -->
   
       <?php } ?>
   
       <?php else { ?>
   
       <p>
         Error.
       </p>
   
       <?php ; } ?>
   
       <?php endif; ?>
   
       <?php endwhile; ?>
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/conditional-loop-whats-wrong/#post-3930345)
 * The main problem is `$wp_query->current_post` is not the post ID which it appears
   you think it is. It is an arbitrary index number into the current query object.
   You should be able to use $post->ID in this context.
 * If I may make a couple suggestions, you don’t need to follow them if you don’t
   want to, it’s not changing anything that’s wrong.
 * Consider using in_array() instead of chaining a bunch of equivalences together
   with ORs. As in `if (in_array($post->ID, array(2,3,4))`.
 * Instead of `<?php } ?> <?php elseif(` just do `<?php } elseif(`, it’s less cluttered
   and more clear the elseif relates to the previous {} block.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/conditional-loop-whats-wrong/#post-3930350)
 * > What am I doing wrong, here?
 * what is the result?
    what do you expect?
 * be aware that `$wp_query->current_post` starts with 0 zero for the first post
   in the loop.
 * if you want to apply the `&& !is_paged()` to all of the `$wp_query->current_post
   == ??` then try to add brackets;
 * example:
 *     ```
       <?php elseif ( ( $wp_query->
       current_post == 2 || $wp_query->
       current_post == 3 || $wp_query->
       current_post == 4 ) && !is_paged() ) { ?>
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Conditional Loop: What's wrong?’ is closed to new replies.

## Tags

 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 3 participants
 * Last reply from: [Michael](https://wordpress.org/support/users/alchymyth/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/conditional-loop-whats-wrong/#post-3930350)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
