First and Last Post on a Page

If you want to detect the first and last post on a page (not the entire query), you can use the code below:

if (have_posts()) : while (have_posts()) : the_post();
  $c++;
  if( $c == 1 ) {
    $style='first';
  } elseif ( $c == $wp_query->post_count ) ) {
    $style='last';?>
  }
  // Rest of the code for the Loop

Leave a Reply

Your email address will not be published. Required fields are marked *

*