-
-
Save banago/5603826 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Infinite next and previous post looping in WordPress | |
*/ | |
if( get_adjacent_post(false, '', true) ) { | |
previous_post_link('%link', '← Previous Post'); | |
} else { | |
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post(); | |
echo '<a href="' . get_permalink() . '">← Previous Post</a>'; | |
wp_reset_query(); | |
}; | |
if( get_adjacent_post(false, '', false) ) { | |
next_post_link('%link', 'Next Post →'); | |
} else { | |
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post(); | |
echo '<a href="' . get_permalink() . '">Next Post →</a>'; | |
wp_reset_query(); | |
}; |
@danidanidantas it is possible, according to the documentation for get_adjacent_post https://codex.wordpress.org/Function_Reference/get_adjacent_post
@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
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();
};
?>
great! thanks.
Try this amouratoglou, but replace the post type.
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', '← Previous project');
} else {
$last = new WP_Query('post_type=project&posts_per_page=1&order=DESC'); $last->the_post();
echo '<a href="' . get_permalink() . '">← Previous project</a>';
wp_reset_query();
};
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', 'Next project →');
} else {
$first = new WP_Query('post_type=project&posts_per_page=1&order=ASC'); $first->the_post();
echo '<a href="' . get_permalink() . '">Next project →</a>';
wp_reset_query();
};
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.
This was super helpful and worked like a charm. Thanks!
Hi everyone, @amouratoglou I had the same problem. Thanks @cabrailsford for the heads up.
Heres the fix:
` <?php
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', '← Previous project');
} else {
$first = new WP_Query('post_type=CHANGETHIS&posts_per_page=1&order=ASC'); $first->the_post();
echo '<a href="' . get_permalink() . '">← Previous project</a>';
wp_reset_postdata();
};
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', 'Next project →');
} else {
$last = new WP_Query('post_type=CHANGETHIS&posts_per_page=1&order=DESC'); $last->the_post();
echo '<a href="' . get_permalink() . '">Next project →</a>';
wp_reset_postdata();
};
?>`
Hi)
this work for me (don't forget change post type):
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '← %title');
} else {
$first = new WP_Query('post_type=projects&posts_per_page=1&order=DESC'); $first->the_post();
echo '<a href="' . get_permalink() . '">← '. get_the_title() .'</a>';
wp_reset_query();
};
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', '%title →');
} else {
$last = new WP_Query('post_type=projects&posts_per_page=1&order=ASC');
$last->the_post();
echo '<a href="' . get_permalink() . '">'. get_the_title() .' →</a>';
wp_reset_query();
};
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