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

For example, inside the loop could i do something like this

if lastpost { 
else {
share|improve this question
if ($wp_query->current_post +1 == $wp_query->post_count) {
    // this is the last post

Change $wp_query to your own query variable if you made a new WP_Query object.

share|improve this answer

I've coded up a quick little example for you. Should explain how to get the first and last post in a WP loop.

    $post_count = 0;
    $total = count($posts);

    while (have_posts()) : the_post();

        if ($post_count == 1 AND $post_count !== $total)
            // This is the first post

        if ($post_count == $total)
            // This is the last item


share|improve this answer

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.