Wizard
Wizard

Reputation: 11265

wordpress show same posts in each page pagination

I made pagination but, it not work corrent, in each page showing same posts.

Code:

<?php if ( have_posts() ) : ?>
    <?php $query = new WP_Query( array('post_type' => 'post', 'kalba' => 'Lietuviu', 'posts_per_page' => 2) );?>
    <?php while ( $query->have_posts() ) : $query->the_post();?>
        <h2 class="title"><a href ="<?php the_permalink();?>"><?php echo $query->post->post_title;?></a></h2>
        <div class="date"><?php echo mysql2date('Y-m-d', $query->post->post_date);?></div>
        <div class="post"><p><?php echo $query->post->post_content;?></p></div>
    <?php endwhile;?>
    <? $big = 999999999; // need an unlikely integer
        echo paginate_links( array( 'base'    => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                            'format'  => '?paged=%#%',
                            'current' => max( 1, get_query_var( 'paged' ) ),
                            'total'   => $query->max_num_pages,
                            'end_size'=> 1,
                            'mid_size'=> 10 ) );?>
<?php endif; ?>

I can't found problem, maybe problem is that I using "taxonomy".

Upvotes: 1

Views: 1720

Answers (1)

Samuel Cook
Samuel Cook

Reputation: 16828

The problem is that you are executing the same query over and over:

<?php $query = new WP_Query( array('post_type' => 'post', 'kalba' => 'Lietuviu', 'posts_per_page' => 2) );?>

You need to set an offset in the WP_Query array: 'offset'=> 2

We can calculate the offset by using a global variable that wordpress uses called $page by using a simple equation: $offset = ($page -1) * $post_per_page;

so your final WP_QUERY should look something like:

<?php $query = new WP_Query( array('post_type' => 'post', 'kalba' => 'Lietuviu', 'posts_per_page' => 2, 'offset'=> (($page -1) * 2) ) );?>

Upvotes: 1

Related Questions