X
X

Pagination

Loop for paginated posts

1)

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

//oppure (protect against arbitrary paged values):
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(
	'posts_per_page' => 5,
	...,
	'paged' => $paged,
);

$the_query = new WP_Query( $args );
?>

oppure 2)
https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops

$custom_query_args = array(
	..., 
);

// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

If your page is a static front page, be sure to use ‘page’ instead of ‘paged’ as a static front page uses page and not paged. This is what you should have for a static front page:

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

//oppure: 
$custom_query_args['paged'] = get_query_var( 'page' ) ? get_query_var( 'page' )  : 1; )

Esempio (remember $front_query->max_num_pages)

<?php
 
 $paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
 $args = array(
  'post_type' => 'post',
  'paged' => $paged,
 );
 $front_query = new WP_query( $args );
 
 if ( $front_query->have_posts() ) : ?>
  
  <ul class="front-query grid"><li class="grid-sizer"></li><li class="gutter-sizer"></li>

  <?php while ( $front_query->have_posts() ) :
  $front_query->the_post(); ?>
   <li>
    <figure><?php echo get_the_post_thumbnail(); ?></figure>
    <div class="post-info"><?php the_title(); ?></div>
    <div class="info-toggle"><a href="#">Info</a></div>
   </li>
  <?php endwhile; ?>
  
  </ul>
  
  <!-- Start the pagination functions after the loop. -->
  <div class="nav-previous alignleft"><?php next_posts_link( 'Older Entries', $front_query->max_num_pages ); ?></div>
  <div class="nav-next alignright"><?php previous_posts_link( 'Newer Entries' ); ?></div>
  <!-- End the pagination functions after the loop. -->
  
  <?php wp_reset_postdata(); ?>
  
 <?php endif;
?>