Last active
May 29, 2023 16:50
-
-
Save alana-mullen/7312492 to your computer and use it in GitHub Desktop.
Detect the last post in the WordPress loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} ?> |
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
Thank you so much!!
+1
Nice 👍
+1
or,
if (!previous_post_link()) { echo 'the first post here'; }
if (!next_post_link()) { echo 'the last post here'; }
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.
+1
+1
+1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1