2

I want to be able to give the first post in a custom instance of wp_query different formatting to the rest.

This is a sample of my current loop (With some bits simplified to shorten my code);

$args1 = array(
    'post_type' => 'post',
    'posts_per_page' => 4
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
    echo '<h2><a href="'.the_permalink().'">'.the_title().'</a></h2>';
endwhile; wp_reset_postdata();

But for the first post in this loop I want to formatting to be different so that I can show the post thumbnail, a short content excerpt, etc.

2

Hope I'm understanding you correctly could you not just do something simple like this

$args1 = array(
    'post_type' => 'post',
    'posts_per_page' => 4
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : 
    $loop->the_post();
    echo '<h2><a href="'.the_permalink().'">'.the_title().'</a></h2>';
    if ($loop->current_post == 0) {
          echo the_post_thumbnail();
          echo the_excerpt();
    }
endwhile; wp_reset_postdata();
  • 3
    You don't have to create your own variable to count posts. wp_query has current_post which does exactly the same ;) – Krzysiek Dróżdż Jul 29 '13 at 11:49
  • Thanks I didn't know that have updated with your suggestion. – Mark Davidson Jul 29 '13 at 11:52

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.