Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 59 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • 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';
} ?>
@ronaldaraujo
Copy link

+1

@alexandremaeda
Copy link

+1

@ottobyte
Copy link

+1

@sinashamsizadeh
Copy link

+1

@jemoreto
Copy link

+1

Copy link

ghost commented Jan 25, 2018

+1

@pareshsojitra
Copy link

+1

@mikeviele
Copy link

+1

@StevenCombes
Copy link

+1

@acenkus
Copy link

acenkus commented May 1, 2018

+1

@nr1q
Copy link

nr1q commented May 15, 2018

+1

@irwinbraganza
Copy link

+1

@ronjramos
Copy link

+1

@RaiBrightfox
Copy link

+1

@manuelricci
Copy link

+1

@jamiecalabro
Copy link

+1

@chpruckner
Copy link

Thank you so much!!
+1

@harnerdesigns
Copy link

Nice 👍

@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