0

I've enabled pagination for one of my pages where I'm pulling the latest news articles from. This is the code:

<div class="container white" style="padding:50px 0;">
    <div class="container-max">
        <?php
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';

        $args = array(
            'numberposts' => 5,
            'offset' => 2,
            'post_status'=>"publish",
            'post_type'=>"post",
            'orderby'=>"post_date",
            'cat'=>'-8, -9, -7, -6, -5, -4',
            'paged'=> $paged
        );

        $postslist = get_posts( $args );
        ?>

            <div class="latest_new_posts main-news">

                <?php
                $wp_query = new WP_Query('$args'.$paged);

                    if ( $wp_query->have_posts() ) :

                        while ( $wp_query->have_posts() ) : the_post();

                            foreach ($postslist as $post) :  setup_postdata($post);
                ?>

                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="position:relative; margin:45px 0;" >     <div class="blog-date-news main-news-date"><span><?php the_date('d/m/Y'); ?></span></div>   <div class="blog-container main-news-container">    <div class="news-blog-title"><span><?php the_title(); ?></span></div>   <div class="news-blog-excerpt"> <?php echo excerpt(500); ?> </div>      <div class="news-blog-more main-news-blog-more"><a href="<?php the_permalink(); ?>"> <img src=""/></a></div> </div>    </div>

                <?php
                            endforeach;

                        endwhile;
                ?>

As you can see I've set 'offset' => 2 as the first 2 posts shouldn't be displayed. Now this work, however when you go to the second page the last 2 posts from the first page show again. So for example I've got 4 posts on page 1: Post A, Post B, Post C and Post D On the second page it will then show: Post C, Post D, Post E, Post F

This doesn't happen when you go from the second to the third page.

Anybody know how to fix this?

1
  • The offset argument is known to break pagination in WordPress and indeed there is a fix. I could explain it all here but there is a topic on it in the WordPress codex: codex.wordpress.org/…
    – berend
    Jul 6, 2017 at 11:58

1 Answer 1

-1

You can try using this code.its perfect working and already tested.

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged == "1") {
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'offset' => 0,
        'posts_per_page' => 5
    );
} else {
    $offset = $paged * 5;
    $offset = $offset - 5;
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'offset' => $offset,
        'posts_per_page' => 5
    );
}

$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();

            /*Your Code */

endwhile;
?>
<div class="pagination-grp">
    <?php
    $big = 999999999; // need an unlikely integer
    //$i=1;

    echo paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'prev_text' => __('<'),
        'next_text' => __('>'),
        'total' => $loop->max_num_pages

    ));
    wp_reset_postdata();
    endif;
    ?>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.