For a single custom post type, I’m using previous_post_link and next_post_link with simple arrows. (Although text arrows are shown here, in the actual I’m using graphics with the text hidden via CSS.) Code:
<nav class="arrowNav">
<div class="nav-previous">
<?php previous_post_link('%link', '«'); ?>
</div>
<div class="nav-next">
<?php next_post_link('%link', '»'); ?>
</div>
</nav>
This works fine except for the edge cases. I’d like the posts to wrap around: that is, for the first post, I’d like previous_post_link to link to the last post, and for the last post next_post_link would link to the first post. It appears the default for both those cases is null. How do I test for first and last post status and produce links for each?
Add following custom functions in functions.php file and instead of calling previous_post_link and next_post_link functions call custom_next_post_link and custom_previous_post_link custom functions respectively.
The accepted answer works, but probly should have used get_posts() instead of a whole wpquery and loop:
It’s generally frowned upon to use wpquery if you dont need to.
With full credit, otherwise, to Vinod Dalvi.