loop - Pagination go to first page if i'm on last post

admin2025-06-02  3

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)
    ),
);

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)
    ),
);
Share Improve this question asked Mar 17, 2013 at 20:57 dansigedansige 1235 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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 ) ); ?>"><?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; ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748869541a314398.html

最新回复(0)