Languages: English • 日本語 (Add your language)
Gets a link to the previous set of posts within the current query.
Because post queries are usually sorted in reverse chronological order, get_next_posts_link() usually points to older entries (toward the end of the set) and get_previous_posts_link() usually points to newer entries (toward the beginning of the set).
Note: See Troubleshooting Broken Pagination if you have pagination issues.
<?php echo get_next_posts_link( $label, $max_page ); ?>
Link to next page if successful, otherwise null.
<?php echo get_next_posts_link(); ?>
<?php echo get_next_posts_link('Go to next page'); ?>
<?php echo get_next_posts_link('Go to next page',4); ?>
Add the $max_pages parameter to the get_next_posts_link() 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.
<?php // set the "paged" parameter (use 'page' if the query is on a static front page) $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; // the query $the_query = new WP_Query( 'cat=1&paged=' . $paged ); ?> <?php if ( $the_query->have_posts() ) : ?> <?php // the loop while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php the_title(); ?> <?php endwhile; ?> <?php // get_next_posts_link() usage with max_num_pages echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); echo get_previous_posts_link( 'Newer Entries' ); ?> <?php // clean up after our query wp_reset_postdata(); ?> <?php else: ?> <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?>
Since 2.7.0
get_next_posts_link() is located in wp-includes/link-template.php
.