paginate_comments_links()WP 2.7.0

Displays or retrieves pagination links for the comments on the current post.

No Hooks.

Return

null|String|Array. Void if 'echo' argument is true and 'type' is not an array, or if the query is not for an existing single post of any post type. Otherwise, markup for comment page links or array of comment page links, depending on 'type' argument.

Usage

paginate_comments_links( $args );
$args(string|array)
Optional args. See paginate_links().
Default: empty array

Examples

0

#1 Output page navigation of comments

If the WP settings are set to "separate comments into pages", then the template uses this code to display the navigation:

<div class="navigation"><?php paginate_comments_links( $args ) ?></div>

We end up with a code like this:

<div class="navigation">
	<a class="prev page-numbers" href="/id_140/comment-page-3#comments">«««</a>
	<a class='page-numbers' href='/id_140/comment-page-1#comments'>1</a>
	<a class='page-numbers' href='/id_140/comment-page-2#comments'>2</a>
	<a class='page-numbers' href='/id_140/comment-page-3#comments'>3</a>
	<span class='page-numbers current'>4</span>
</div>
0

#2 Changing the text of "next/previous page" links

To change the text of the next/previous page links, you must pass the arguments prev_text and next_text

paginate_comments_links( 'prev_text=back&next_text=forward' )

If you want to use special html symbols (html entities) in the link texts, you have to pass the arguments through an array:

paginate_comments_links( [
	'prev_text' => '«', 
	'next_text' => '»'
] );

Notes

  • See: paginate_links()
  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 2.7.0 Introduced.

paginate_comments_links() code WP 6.5.3

function paginate_comments_links( $args = array() ) {
	global $wp_rewrite;

	if ( ! is_singular() ) {
		return;
	}

	$page = get_query_var( 'cpage' );
	if ( ! $page ) {
		$page = 1;
	}
	$max_page = get_comment_pages_count();
	$defaults = array(
		'base'         => add_query_arg( 'cpage', '%#%' ),
		'format'       => '',
		'total'        => $max_page,
		'current'      => $page,
		'echo'         => true,
		'type'         => 'plain',
		'add_fragment' => '#comments',
	);
	if ( $wp_rewrite->using_permalinks() ) {
		$defaults['base'] = user_trailingslashit( trailingslashit( get_permalink() ) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged' );
	}

	$args       = wp_parse_args( $args, $defaults );
	$page_links = paginate_links( $args );

	if ( $args['echo'] && 'array' !== $args['type'] ) {
		echo $page_links;
	} else {
		return $page_links;
	}
}