next_posts_link()WP 0.71

Displays the next posts page link.

No Hooks.

Return

null. Nothing (null).

Usage

next_posts_link( $label, $max_page );
$label(string)
Content for link text.
Default: null
$max_page(int)
Max pages.

Examples

0

#1 Display a link to the posts next page :

<?php next_posts_link(); ?>
0

#2 Change the link anchor to "moving on":

<?php next_posts_link( 'move on', 0 ); ?>
0

#3 Usage when querying the loop with WP_Query

Add the $max_pages parameter to this function when querying the loop with WP_Query. To get the total amount of pages you can use the max_num_pages property of the custom WP_Query object.

// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = get_query_var( 'paged' ) ?: 1;

// the query
$the_query = new WP_Query( [
	'cat'   => 1,
	'paged' => $paged
] );

if ( $the_query->have_posts() ) {

	while ( $the_query->have_posts() ) {
		$the_query->the_post();

		the_title();
	}

	// next_posts_link() usage with max_num_pages.
	next_posts_link( __( 'Older', 'textdomain' ), $the_query->max_num_pages );
	previous_posts_link( __( 'Newer', 'textdomain' ) );

	// Clean up after the query and pagination.
	wp_reset_postdata(); 
}
else {
	?>
	<p><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ) ); ?></p>
	<?php
}

Changelog

Since 0.71 Introduced.

next_posts_link() code WP 6.4.3

function next_posts_link( $label = null, $max_page = 0 ) {
	echo get_next_posts_link( $label, $max_page );
}