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

2
  • 1
    your offset is creating the issue, Sep 5, 2021 at 6:45
  • @RajneeshTiwari yes, you're correct. I fixed it and posted the code below if anyone's faced the same and need the solution. Sep 5, 2021 at 7:54

1 Answer 1

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();
?>

Your Answer

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

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