Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aaronsummers/50b78bcfedbf0a9e10e87acf8405ae4f to your computer and use it in GitHub Desktop.
Save aaronsummers/50b78bcfedbf0a9e10e87acf8405ae4f to your computer and use it in GitHub Desktop.
Infinite next and previous post looping in WordPress
<?php
/**
* Infinite next and previous post looping in WordPress
*/
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '&larr; Previous Post');
} else {
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
echo '<a href="' . get_permalink() . '">&larr; Previous Post</a>';
wp_reset_query();
};
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Post &rarr;');
} else {
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
echo '<a href="' . get_permalink() . '">Next Post &rarr;</a>';
wp_reset_query();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment