Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
};
@danidanidantas

This comment has been minimized.

danidanidantas commented May 6, 2016

Its possible use a cpt and show only same taxonomies?

@treetrum

This comment has been minimized.

treetrum commented May 9, 2016

Awesome! Thanks for this!

I took inspiration from it and made my own version that returns the actual post object rather than a link.

If you're interested:
https://gist.github.com/samjdavis13/3a843c961aed69c732f0a61da854b16d

@camilobuitrago

This comment has been minimized.

camilobuitrago commented Jun 15, 2016

@danidanidantas it is possible, according to the documentation for get_adjacent_post https://codex.wordpress.org/Function_Reference/get_adjacent_post

@camilobuitrago

This comment has been minimized.

camilobuitrago commented Jun 16, 2016

@danidanidantas I was searching for the same solution, you don't do it with get_adjacent_post, you add the following to the WP_Query string &post_type=name-of-post-type

@amouratoglou

This comment has been minimized.

amouratoglou commented May 17, 2018

Is not working for me ... the last post links to itself.

Any suggestions?

<?php 
/**
 *  Infinite next and previous post looping in WordPress
 */
if( get_adjacent_post(false, '', true) ) { 
	previous_post_link('%link', '<i class="fa fa-angle-left"></i> Previous Brand');
} else { 
    $first = new WP_Query('post_type=product_bands&posts_per_page=1&order=DESC'); $first->the_post();
    	echo '<a href="' . get_permalink() . '"><i class="fa fa-angle-left"></i> Previous Brand</a>';
  	wp_reset_query();
}; 

    
if( get_adjacent_post(false, '', false) ) { 
	next_post_link('%link', 'Next Brand <i class="fa fa-angle-right"></i>');
} else { 
	$last = new WP_Query('post_type=product_bands&posts_per_page=1&order=ASC'); $last->the_post();
    	echo '<a href="' . get_permalink() . '">Next Brand <i class="fa fa-angle-right"></i></a>';
    wp_reset_query();
}; 

?>
@davidsolsona

This comment has been minimized.

davidsolsona commented Jul 23, 2018

great! thanks.

@ragudesign

This comment has been minimized.

ragudesign commented Aug 2, 2018

Try this amouratoglou, but replace the post type.

if( get_adjacent_post(false, '', false) ) { 
	next_post_link('%link', '&larr; Previous project');
} else { 
	$last = new WP_Query('post_type=project&posts_per_page=1&order=DESC'); $last->the_post();
	echo '<a href="' . get_permalink() . '">&larr; Previous project</a>';
	wp_reset_query();
}; 


if( get_adjacent_post(false, '', true) ) { 
	previous_post_link('%link', 'Next project &rarr;');
} else { 
	$first = new WP_Query('post_type=project&posts_per_page=1&order=ASC'); $first->the_post();
	echo '<a href="' . get_permalink() . '">Next project &rarr;</a>';
	wp_reset_query();
}; 
@cabrailsford

This comment has been minimized.

cabrailsford commented Oct 25, 2018

Just as a head's up, you should probably be using wp_reset_postdata(), not wp_reset_query(), since the latter is typically only used if you're using query_posts and not WP_Query.

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