Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the following code to print out the last 8 published post in columns of 3 so 3 column and 3 rows :

<?php
  $args = array(
               'post_type' => 'post',
               'posts_per_page' => 8,
               'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
               );
  query_posts($args);
  $x = 0;
  while (have_posts()) : the_post(); ?>    

    <?php if($x == 2) { ?>
    <div class="portfolio_list_box portfolio_list_last">
    <?php } else { ?>
    <div class="portfolio_list_box">
    <?php } ?>
      <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('featured-home-portfolio'); ?></a>
      <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
      <?php the_excerpt(); ?>
    </div><!--//portfolio_list_box-->  

    <?php if($x == 2) { echo '<div class="clear"></div>'; $x = -1; } ?>

  <?php $x++; ?>
  <?php endwhile; ?>
  <?php wp_reset_query(); ?> 

now I want to place an image after 5th post . which is row 2 column 3. I modified this code for so many times but I can not get that image to show up after post 5 only.

I added this :

<?php if($x == 2) { ?>
    <div class="portfolio_list_box portfolio_list_last">
    <?php } elseif($x == 5) { ?>
 // image 
<?php } else { ?>

but does not work . I really appreciate if someone help me to figure out this. thanks

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Multiple nested if statements can create a mess you might look into using a switch for this case.

Something like:

  //your $args
  $the_switch_query = new WP_Query($args);

  while ( $the_switch_query ->have_posts() ) : $the_switch_query ->the_post(); 

  $query_number = $the_switch_query->current_post + 1; //count them

       switch ($query_number){

             case 1;
             echo "post one";
             break;

             case 5;
             echo "post five";
             break;

            // more cases

            default:
            echo "default";
        }
       //rest of your stuff
share|improve this answer
    
thanks for your answer, I am not even sure have to use this code , is it possible to help me a little bit more ? thanks –  John Smith Oct 23 '12 at 18:01
    
This might help, php.net/manual/en/control-structures.switch.php –  Wyck Oct 23 '12 at 20:51
    
thanks for the help ;) –  John Smith Oct 23 '12 at 21:55

this is really just a php question. it never reaches 5 because you reset $x to -1 when it's equal to 2

<?php if($x == 2) { echo '<div class="clear"></div>'; $x = -1; } ?>
share|improve this answer
    
if I change the $x=-1 to any value, the columns will be apart –  John Smith Oct 23 '12 at 18:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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