the_post_navigation()WP 4.1.0

Displays the HTML block of links to the next and previous posts. Uses on a single post page: is_singular().

This function combines next_post_link() and previous_post_link() into a single function. The function is created for convenience in order to insert a navigation block between adjacent articles (posts) in one line.

To get the HTML code for processing, use get_the_post_navigation()

Not to be confused with the_posts_navigation(), which displays links to the next/previous page of set of posts, for example on category page.

No Hooks.

Return

null. Nothing (null). Displays HTML code.

Usage

<?php the_post_navigation( $args ); ?>
$args(array)

Array that can contain the following parameters:

  • $prev_text(string)
    Anchor text to display in the previous post link.
    Default: '%title'

  • $next_text(string)
    Anchor text to display in the next post link.
    Default: '%title'

  • $in_same_term(true/false)
    Whether link should be in a same taxonomy term.
    Default: false

  • $excluded_terms(array/string)
    Array or comma-separated list of excluded term IDs.
    Default: ''

  • $taxonomy(string)
    Taxonomy, if $in_same_term is true.
    Default: 'category'

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

Default: array()

Examples

0

#1 Navigation links between posts

Let's output links to the next and previous entries (posts) on a separate page of is_single() type.

<?php
// next/prev. post
the_post_navigation( array(
	'prev_text' => '<span class="meta-nav" aria-hidden="true">Backward</span> ' .
		'<span class="screen-reader-text">Previous entry</span> ' .
		'<span class="post-title">%title</span>',
	'next_text' => '<span class="meta-nav" aria-hidden="true">Further</span> ' .
		'<span class="screen-reader-text">Next entry</span> ' .
		'<span class="post-title">%title</span>',
) );
?>

As a result, we get such HTML code:

<nav class="navigation post-navigation" role="navigation">
	<h2 class="screen-reader-text">Posts navigation</h2>
	<div class="nav-links">
		<div class="nav-previous">
			<a href="//example.com/post8/" rel="prev">
				<span class="meta-nav" aria-hidden="true">Backward</span>
				<span class="screen-reader-text">Previous entry</span>
				<span class="post-title">Some Post Title</span>
			</a>
		</div>
		<div class="nav-next">
			<a href="//example.com/post10/" rel="next">
				<span class="meta-nav" aria-hidden="true">Further</span>
				<span class="screen-reader-text">Next entry</span>
				<span class="post-title">Hello World!</span>
			</a>
		</div>
	</div>
</nav>

Changelog

Since 4.1.0 Introduced.

the_post_navigation() code WP 6.4.3

function the_post_navigation( $args = array() ) {
	echo get_the_post_navigation( $args );
}