I have a custom post type and used get_next_post() and get_previous_post() to create a simple pagination. However i need to create a pagination loop (if that is the correct term). If i’m on the first post i need to browse back to the last post and if i’m on the last post i need to browse to the first post. Is there a simple way to do this that i haven’t seen but Google, this site and the codex has not been very helpfull :/
The current code i have is. I’ve tried to use wp_list_pages and other functions but with little luck.
$next_post = get_next_post();
$prev_post = get_previous_post();
$nav = array(
"next_post" => array(
"url" => get_permalink($next_post->ID),
"id" => $next_post->ID,
"titill" => get_field("vinstri_titill", $next_post->ID)." - ".get_field("vinstri_undirtitill", $next_post->ID)
),
"prev_post" => array(
"url" => get_permalink($prev_post->ID),
"id" => $prev_post->ID,
"titill" => get_field("vinstri_titill", $prev_post->ID)." - ".get_field("vinstri_undirtitill", $prev_post->ID)
),
);
Here is one idea for the previous post circle:
where we have defined this function:
and the filter function to remove the specific date comparison:
The aim is to emulate the
get_previous_post()
function with the same input parameters, but you could of course play withget_post()
in your code instead to create the closed loop.My solution if next is not empty give me loop, but only the first with permalink: