0

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

1

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
| improve this answer | |
1

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; } ?>
| improve this answer | |
  • if I change the $x=-1 to any value, the columns will be apart – Andy M Oct 23 '12 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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