0

I am trying to add the pagination to my website but each page has the same posts. I tried that code to set an automatic offset but it's not working. It keeps inserting only the first 3 posts even on page 2 and 3

function pag_in( $query ) {
$page = get_query_var( 'page' ) ? absint( get_query_var( 'page' ) ) : 1;


$offset = ($page - 1) * 3;
  
    $query->set( 'posts_per_page', 3 );
   $query->set( 'offset', $offset);
    $query->set( 'order', 'ASC');
        }  add_action( 'pre_get_posts', 'pag_in', 1 );

1 Answers1

0

Maybe get_query_var( 'page' ) is returning null.

$current_page = get_query_var('paged');
$current_page = max( 1, $current_page );

$per_page = 12;
$offset_start = 1;
$offset = ( $current_page - 1 ) * $per_page + $offset_start;
AaronJ
  • 1,060
  • 10
  • 24