0

This question has asked many time before and none has answers my question. I went through most of the answers before post this.

I have a pagination query in home page. Pagination is showing and I can going through the pages. But same posts are displaying for every paginate link.

This is my code so far. Can anybody elaborate the wrong here?

<?php
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    $args = array(
        'post_type'=>'post', 
        'posts_per_page'=>10, 
        'orderby'=>'date', 
        'order'=>'DESC', 
        'offset'=>3, 
        'paged'=>$paged
    );
    $blogs = new WP_Query($args);
    if($blogs->have_posts()):
        while($blogs->have_posts()): $blogs->the_post();
?>

    <div class="col-md-6">
        <div class="single-post">
            <div class="thumb">
                <a href="<?php the_permalink(); ?>">
                    <img src="<?php the_post_thumbnail_url('blog-thumbnail'); ?>" alt="<?php the_title(); ?>" class="img-fluid" />
                </a>
            </div>
            <div class="content">
                <div class="category">
                    <?php
                    $categories = get_the_category();
                    foreach ($categories as $key => $category) {
                        echo '<a href="' . get_category_link($category->term_id). '">'. $category->name. '</a>';
                    }
                    ?>
                </div>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <div class="metadata">
                    <span class="date"><?php echo get_the_date(); ?></span>
                    <span class="author"><?php echo get_the_author(); ?></span>
                </div>
                <div class="excerpt">
                    <?php the_excerpt(); ?>
                </div>
            </div>
        </div>
    </div>

<?php
        endwhile;
        echo '<div class="paginate-wrap">'. paginate_links() . '</div>';
    endif;
    wp_reset_query();
?>

Thanks in advance

WP Learner
  • 788
  • 1
  • 13
  • 34

1 Answers1

0

Found the solution. Is anybody facing the same please check this.

My code is right, but because of the offset parameter pagination is not working. I found the solution here.

So, this is my final code:

<?php
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

    $per_page = 10;
    $defualt_offset = $offset;

    if ($paged == 1) {
        $offset = $default_offset;
    } else {
        $offset = (($paged - 1) * $per_page) + $default_offset;
    }

    $args = array(
        'post_type'=>'post', 
        'posts_per_page'=>$per_page, 
        'orderby'=>'date', 
        'order'=>'DESC', 
        'offset'=>3, 
        'paged'=>$paged
    );

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

    <div class="col-md-6">
        <div class="single-post">
            <div class="thumb">
                <a href="<?php the_permalink(); ?>">
                    <img src="<?php the_post_thumbnail_url('blog-thumbnail'); ?>" alt="<?php the_title(); ?>" class="img-fluid" />
                </a>
            </div>
            <div class="content">
                <div class="category">
                    <?php
                    $categories = get_the_category();
                    foreach ($categories as $key => $category) {
                        echo '<a href="' . get_category_link($category->term_id). '">'. $category->name. '</a>';
                    }
                    ?>
                </div>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <div class="metadata">
                    <span class="date"><?php echo get_the_date(); ?></span>
                    <span class="author"><?php echo get_the_author(); ?></span>
                </div>
                <div class="excerpt">
                    <?php the_excerpt(); ?>
                </div>
            </div>
        </div>
    </div>

<?php
        endwhile;
        echo '<div class="paginate-wrap">'. paginate_links() . '</div>';
    endif;
    wp_reset_query();
?>
WP Learner
  • 788
  • 1
  • 13
  • 34