Skip to content

Instantly share code, notes, and snippets.

@krishna19
Created November 22, 2015 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krishna19/1f959f0365834f173b1c to your computer and use it in GitHub Desktop.
Save krishna19/1f959f0365834f173b1c to your computer and use it in GitHub Desktop.
get previous/next post links
<?php
/**
* Infinite next and previous post looping in WordPress
*/
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
// get the first posts ID etc
$args = array('post_type'=>'products', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID;
// get the last posts ID etc
$last_post = end($posts);
$last_id = $last_post->ID; // To get ID of last post in custom post type outside of loop
if($previous_id) {
$prev_post_url = get_permalink($previous_id);
} else {
$prev_post_url = get_permalink($first_id);
};
if($next_id) {
$next_post_url = get_permalink($next_id);
} else {
$next_post_url = get_permalink($last_id);
} ?>
<div class="wrapper paging">
<a href="<?php echo $prev_post_url; ?>" class="prev-product"></a>
<a href="<?php echo $next_post_url; ?>" class="next-product"></a>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment