WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I understand that there is a way to have the loop display multiple ways on one page without using multiple loops... Or in other words something like:

<?php if (have_posts()) : while (have_posts()) : ?>
<!-- Code for the First Post -->
<?php else: ?>
<!-- Code for Posts 2-4 -->
<?php else: ?>
<!-- Code for Posts 5-12 -->
<!-- stuff to close loop -->

Problem is that I don't really know php well, and I have no idea how to have it count posts and how to tell it for the first post do X, for posts 2 through 4 do X, and for posts 5 through 12 do X... If anyone can point me to a clear tutorial or let me know what PHP is necessary, I'd really appreciate it.

Thanks in advance!

share|improve this question
You should read about if/else/while/for/foreach on php.net first. This surely helps you getting 90% of your Qs out of your way in less than 20 minutes. – kaiser Jan 19 '12 at 4:09
I understand how if/else statements themselves work, it's just I don't understand how telling Wordpress to count and for x# of posts works... – cw02 Jan 19 '12 at 13:40

this is your answer... however like Kaiser said, you should really take some time to read a little...

 if ( have_posts() ) while ( have_posts() ) : the_post();
      if($i==0) { ?>


      } else if(($i==1) || ($i==2) || ($i==3)) {

         // POST 2, 3, 4

      } else {

         // everything else


This is a fast answer and is not the ideal approach, but it will solve your problem...

share|improve this answer
Thanks, this helps a lot. I've been trying to read but I'm having difficulty understanding how counting works... And php.net isn't exactly easy to understand. – cw02 Jan 19 '12 at 14:12
if the answer worked for you, please remember to mark it, so other people will be able to find a solution for similar problems – andresmijares25 Jan 19 '12 at 15:40

Your Answer


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.