get_the_comments_navigation()WP 4.4.0

Retrieves navigation to next/previous set of comments, when applicable.

1 time — 0.00276 sec (very slow) | 50000 times — 5.96595 sec (fast)

No Hooks.

Return

String. Markup for comments links.

Usage

get_the_comments_navigation( $args );
$args(array)

Optional.

Default: comments navigation arguments

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

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

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

  • aria_label(string)
    ARIA label text for the nav element.
    Default: 'Comments'

  • class(string)
    Custom class for the nav element.
    Default: 'comment-navigation'

Examples

0

#1 Demo

Suppose the post has 44 comments and we are on the second page of the comment pagination, then:

$html = get_the_comments_navigation();

$html will contain:

<nav class="navigation comment-navigation" role="navigation">

	<h2 class="screen-reader-text">Comments Navigation</h2>

	<div class="nav-links">
		<div class="nav-previous">
			<a href="http://example.com/foo/comment-page-1#comments">Previous comments</a>
		</div>
		<div class="nav-next">
			<a href="http://example.com/foo/comment-page-3#comments">Next comments</a>
		</div>
	</div>

</nav>

Changelog

Since 4.4.0 Introduced.
Since 5.3.0 Added the aria_label parameter.
Since 5.5.0 Added the class parameter.

get_the_comments_navigation() code WP 6.4.3

function get_the_comments_navigation( $args = array() ) {
	$navigation = '';

	// Are there comments to navigate through?
	if ( get_comment_pages_count() > 1 ) {
		// Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
		if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
			$args['aria_label'] = $args['screen_reader_text'];
		}

		$args = wp_parse_args(
			$args,
			array(
				'prev_text'          => __( 'Older comments' ),
				'next_text'          => __( 'Newer comments' ),
				'screen_reader_text' => __( 'Comments navigation' ),
				'aria_label'         => __( 'Comments' ),
				'class'              => 'comment-navigation',
			)
		);

		$prev_link = get_previous_comments_link( $args['prev_text'] );
		$next_link = get_next_comments_link( $args['next_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, $args['class'], $args['screen_reader_text'], $args['aria_label'] );
	}

	return $navigation;
}