Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alana-mullen/7312492 to your computer and use it in GitHub Desktop.
Save alana-mullen/7312492 to your computer and use it in GitHub Desktop.
Detect the last post in the WordPress loop
<?php if (($wp_query->current_post +1) == ($wp_query->post_count)) {
echo 'This is the last post';
} ?>
<?php if (($wp_query->current_post +1) != ($wp_query->post_count)) {
echo 'This is the not the last post';
} ?>
@imomer
Copy link

imomer commented Apr 11, 2019

+1

@rainb3rry
Copy link

or,
if (!previous_post_link()) { echo 'the first post here'; }
if (!next_post_link()) { echo 'the last post here'; }

@srikat
Copy link

srikat commented Jul 4, 2019

or,
if (!previous_post_link()) { echo 'the first post here'; }
if (!next_post_link()) { echo 'the last post here'; }

get_previous_post_link() and get_next_post_link() should be used instead for the if conditions.

@fearlex
Copy link

fearlex commented Sep 14, 2019

+1

@christianpornela
Copy link

+1

@Rmanaf
Copy link

Rmanaf commented Aug 18, 2020

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment