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)
),
);
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Here is one idea for the previous post circle:
$next_post = get_next_post(); $prev_post = get_previous_post_circle();
where we have defined this function:
function get_previous_post_circle($in_same_cat = false, $excluded_categories = ''){
$prev_post = get_previous_post($in_same_cat,$excluded_categories);
if($prev_post){
return $prev_post;
}else{
add_filter('get_previous_post_where','custom_get_previous_post_where');
$prev_post = get_previous_post($in_same_cat,$excluded_categories);
remove_filter('get_previous_post_where','custom_get_previous_post_where');
if($prev_post){
return $prev_post;
}else{
return '';
}
}
}
and the filter function to remove the specific date comparison:
function custom_get_previous_post_where($where){
$where=" WHERE ".implode(" AND ",array_slice(explode("AND",$where),1));
return $where;
}
The aim is to emulate the get_previous_post() function with the same input parameters, but you could of course play with get_post() in your code instead to create the closed loop.
Method 2
My solution if next is not empty give me loop, but only the first with permalink:
<?php
$next_post = get_previous_post();
if (!empty( $next_post )): ?>
<a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>" rel="nofollow noreferrer noopener"><?php echo esc_attr( $next_post->post_title ); ?></a>
<?php else: ?>
<?php $args = array(
'post_type'=> 'post',
'orderby' => 'ID',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : ?>
<?php while ( $result->have_posts() ) : $result->the_post(); ?>
<?php the_permalink(); ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); endif; ?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0