get_comments_pagenum_link()WP 2.7.0

Gets the link to the specified comment pagination page of the post.

The function is intended for use only on single pages of type is_singular().

The function adds the comment page to the current post URL, taking into account the established permalink settings and settings in $wp_rewrite that relate to comment pages, such as the parameter $wp_rewrite->comments_pagination_base, which usually stores the string comment-page.

1 time — 0.00016 sec (fast) | 50000 times — 5.76 sec (fast)
Hooks from the function

Returns

String. URL to the comment pagination page of the post.

Usage

get_comments_pagenum_link( $pagenum, $max_page );
$pagenum(int)
The page number of the pagination for which the link needs to be obtained.
Default: 1
$max_page(int)
The number of the last comment pagination page, so that no additional part /comment-page-5 is appended to the URL on this page.
Default: 0 (no limits)

Examples

0

#1 Get a link to the comment pagination page of the current page

Suppose we are at http://example.com/page. Then this is how the function will work, with different parameters

echo get_comments_pagenum_link( 5 );
// http://example.com/page/comment-page-5#comments

echo get_comments_pagenum_link( 5, 6 );
// http://example.com/page/comment-page-5#comments

echo get_comments_pagenum_link( 5, 4 );
// http://example.com/page/comment-page-5#comments

echo get_comments_pagenum_link( 5, 5 );
// http://example.com/page#comments

Notes

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 2.7.0 Introduced.

get_comments_pagenum_link() code WP 6.8.3

function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
	global $wp_rewrite;

	$pagenum  = (int) $pagenum;
	$max_page = (int) $max_page;

	$result = get_permalink();

	if ( 'newest' === get_option( 'default_comments_page' ) ) {
		if ( $pagenum !== $max_page ) {
			if ( $wp_rewrite->using_permalinks() ) {
				$result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' );
			} else {
				$result = add_query_arg( 'cpage', $pagenum, $result );
			}
		}
	} elseif ( $pagenum > 1 ) {
		if ( $wp_rewrite->using_permalinks() ) {
			$result = user_trailingslashit( trailingslashit( $result ) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged' );
		} else {
			$result = add_query_arg( 'cpage', $pagenum, $result );
		}
	}

	$result .= '#comments';

	/**
	 * Filters the comments page number link for the current request.
	 *
	 * @since 2.7.0
	 *
	 * @param string $result The comments page number link.
	 */
	return apply_filters( 'get_comments_pagenum_link', $result );
}