Infinite Next and Previous Post Looping in WordPress

Infinite next and previous post looping in WordPress

If you are trying to add next/previous buttons in your post or custom post and want that the very first post also have a previous button pointing to your last post and your last post have a next button pointing to the first post so that every post will have a next and previous button. Then use the below code in single.php file the your theme. If you are using template for showing post or custom post where you want this buttons then add the code there.

if( get_adjacent_post(false, ”, false) ) {

next_post_link(‘%link’, ‘Previous Post’);

} else {

$last = new WP_Query(‘posts_per_page=1&order=ASC’);
echo ‘Previous Post‘;

if( get_adjacent_post(false, ”, true) ) {

previous_post_link(‘%link’, ‘Next Post’);

} else {

$first = new WP_Query(‘posts_per_page=1&order=DESC’);
echo ‘Next Post‘;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at

Up ↑

%d bloggers like this: