get_next_comments_link()WP 2.7.1

Retrieves the link to the next comments page.

1 time — 0.00019 sec (fast) | 50000 times — 2.7 sec (fast)
Hooks from the function

Return

String|null. HTML-formatted link for the next page of comments.

Usage

get_next_comments_link( $label, $max_page, $page );
$label(string)
Label for link text.
Default: ''
$max_page(int)
Max page.
$page(int|null)
Page number.
Default: null

Examples

0

#1 Get a link to the current post comments next page

Let's say the post has 44 comments, and we are on page two of the comments, then:

$link = get_next_comments_link();

/* $link will be equal to
<a href="http://example.com/foo/comment-page-3#comments">Next comments →</a>
*/

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 2.7.1 Introduced.
Since 6.7.0 Added the page parameter.

get_next_comments_link() code WP 6.7.1

function get_next_comments_link( $label = '', $max_page = 0, $page = null ) {
	global $wp_query;

	if ( ! is_singular() ) {
		return;
	}

	if ( is_null( $page ) ) {
		$page = get_query_var( 'cpage' );
	}

	if ( ! $page ) {
		$page = 1;
	}

	$next_page = (int) $page + 1;

	if ( empty( $max_page ) ) {
		$max_page = $wp_query->max_num_comment_pages;
	}

	if ( empty( $max_page ) ) {
		$max_page = get_comment_pages_count();
	}

	if ( $next_page > $max_page ) {
		return;
	}

	if ( empty( $label ) ) {
		$label = __( 'Newer Comments &raquo;' );
	}

	/**
	 * Filters the anchor tag attributes for the next comments page link.
	 *
	 * @since 2.7.0
	 *
	 * @param string $attributes Attributes for the anchor tag.
	 */
	$attr = apply_filters( 'next_comments_link_attributes', '' );

	return sprintf(
		'<a href="%1$s" %2$s>%3$s</a>',
		esc_url( get_comments_pagenum_link( $next_page, $max_page ) ),
		$attr,
		preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label )
	);
}