Show excerpt for only first post in query

I’d like to only show the excerpt for the first post in my main query. I was wondering the best way to approach this.

What I have tried so far is:

  1. Making a custom homepage and using two queries, one that returns 1
    post with an excerpt and another queries that returns posts without.
  2. Just using the default queries but using css to hide the excerpts
    for all posts but the first, using nth-child.

Both of these work well enough, but I was wondering if there was a better way to do this, through a function or filter.

Thanks!

3 Answers
3

You can use $wp_query->current_post in your loop to check the current post. You don’t need two loops, one will do the trick If you need excerpt for first post only, you can do something like this. Just remember, the first post in the loop is 0, and not 1

if ( !$wp_query->current_post > 0 ) :
   the_excerpt();
else :
   <--- DO SOMETHING ELSE FOR OTHER POSTS
endif;

For all the WP_Post member variables, go and check the link provided from the codex

Leave a Comment