WordPress at a glance

get_the_posts_navigation() WP 4.1.0

Retrieve the navigation block to the next/previous set of posts. Used on archive pages (categories, tags).

Such links can be obtained using get_next_posts_link() and get_previous_posts_link() functions.

To get navigation links for a single post, use the_post_navigation().

  • Global. WP_Query. $wp_query WordPress Query object.
Is the basis for: the_posts_navigation()

No Hooks.

Return

String. HTML code for the previous/next sets of posts.

Usage

get_the_posts_navigation( $args );
$args(array)

Default empty array.

  • $prev_text (string)
    Anchor text to display in the previous posts link.
    Default: 'Older posts'

  • $next_text (string)
    Anchor text to display in the next posts link.
    Default: 'Newer posts'

  • $screen_reader_text (string)
    Screen reader text for nav element.
    Default: 'Posts navigation'

Default: posts navigation arguments

Examples

#1 Output links to the previous / next set of posts

the_posts_navigation( [
	'prev_text'          => '← Prev',
	'next_text'          => 'Next →',
	'screen_reader_text' => 'My navigation',
] );
<nav class="navigation posts-navigation" role="navigation">
	<h2 class="screen-reader-text">My navigation</h2>
	<div class="nav-links">
		<div class="nav-previous">
			<a href="http://example.com/page/3/">← Prev</a>
		</div>
		<div class="nav-next">
			<a href="http://example.com/">Next →</a>
		</div>
	</div>
</nav>

Code of get the posts navigation: wp-includes/link-template.php VER 5.1.1

<?php
function get_the_posts_navigation( $args = array() ) {
	$navigation = '';

	// Don't print empty markup if there's only one page.
	if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
		$args = wp_parse_args(
			$args,
			array(
				'prev_text'          => __( 'Older posts' ),
				'next_text'          => __( 'Newer posts' ),
				'screen_reader_text' => __( 'Posts navigation' ),
			)
		);

		$next_link = get_previous_posts_link( $args['next_text'] );
		$prev_link = get_next_posts_link( $args['prev_text'] );

		if ( $prev_link ) {
			$navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
		}

		if ( $next_link ) {
			$navigation .= '<div class="nav-next">' . $next_link . '</div>';
		}

		$navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
	}

	return $navigation;
}

Related Functions

From tag: Navigation pagination

More from category: Menu

No comments
    Hello, !     Log In . Register