• 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

    (@n00bie12)

    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

    (@bcworkz)

    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.

    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.