$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - Pagination go to first page if i'm on last post|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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; ?>
Post a comment

comment list (0)

  1. No comments so far