open
10.10.2022.

The posts pagination

If you don't like the navigation of the previous page of the next page, but want to display it in page numbered order, you don't need a custom plugin. WordPress has this feature!
Note that if you use it on a simple loop or if you use it on wp_query, the code looks different.

// for a simple loop

<?php
the_posts_pagination( array(
'prev_text' => __( '<', 'textdomain' ),
'next_text' => __( '>', 'textdomain' ),
) );
?>

// for wp_query

<?php
$GLOBALS['wp_query']->max_num_pages = $query->max_num_pages;
the_posts_pagination( array(
'prev_text' => __( '<', 'textdomain' ),
'next_text' => __( '>', 'textdomain' ),
) ); ?>

// full paginated wp_query loop

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 12,
'cat' => 1,
'paged'=>$paged
);
$query = new WP_Query($args); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<h2><a href=”<?php the_permalink() ?>” title=”<?php the_title();?>”><?php the_title();?></a></h2>
<?php the_content('read more') ?>

 

<?php endwhile; ?>

 

<?php
$GLOBALS['wp_query']->max_num_pages = $query->max_num_pages;
the_posts_pagination( array(
'prev_text' => __( '<', 'textdomain' ),
'next_text' => __( '>', 'textdomain' ),
) ); ?>

<?php else : ?>
<?php endif; ?>

Copy to Clipboard

Related posts

Got a question? Is something not working? Write and I'll answer!

Leave a Reply

Your email address will not be published. Required fields are marked *