Using offset for custom post query

Situation:

News page – Currently shows the latest 5 news items Archive page – Starts on post 6, I’ve setup pagination.

Read More

For testing purposes I’ve got 20 posts numbered 1 – 20.

<?php 
    $args = array(
     'category' => 7,
      'posts_per_page' => 5, 
      'offset' => ( $paged - 1 ) * $posts_per_page + 5,
     'paged' => $paged );
 ?>

<?php 

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts($args); 
?>

The above code works fine for archive page 1 but when I click page 2 in the pagination it skips 5 and jumps to post 16.

How would I resolve the above query to prevent skipping 5 posts?

Cheers, Dan

Related posts

Leave a Reply

1 comment

  1. So the final code should look like:

    <?php 
    
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts($args); 
    
        $posts_per_page = 5;
    
        $args = array(
         'category' => 7,
          'posts_per_page' => $posts_per_page, 
          'offset' => ( $paged - 1 ) * $posts_per_page ,
         'paged' => $paged );
    
     ?>