Include Numbered Comment Pages in Previous/Next Navigation

Applies to Version(s): Thesis 1

When you have comment pagination enabled in WordPress, Thesis displays additional navigation links for “Previous/Next Comments” above and below the comments listing:

Previous Next Navigation

Default Previous & Next Comment Links

However, WordPress also provides a function named paginate_comment_links(), which not only prints out a link to the next and previous comment pages, but also a numbered list of all the comment pages.

Fortunately, Thesis includes a thesis_comments_navigation filter, which can be used to replace its default comment navigation – allowing us to employ the paginate_comment_links function for a customized look!

First, make sure you have paginated comments enabled in WordPress, under Settings > Discussion.

Then, place the following code in your custom_functions.php file (please see the Note at the end of this article):

function my_comments_nav($nav) {
	if (get_option('page_comments')) { 
		$total_pages = get_comment_pages_count();
		$args = (array(
    			'echo' => false,
    			'prev_text' => '«', 
    			'next_text' => '»',
    			'add_fragment' => '#comments' 
				));
		if ($total_pages > 1) {
			$nav = '<div id="comment_nav" class="prev_next"><p class="previous">';
			$nav .= paginate_comments_links($args);
			$nav .= "</p></div>\n\n";
			}
		}
	return $nav;
}

add_filter('thesis_comments_navigation', 'my_comments_nav');

Now, the “Previous/Next Comments” links will look like this:

Numbered Comment Pages Navigation

Numbered Comment Pages Navigation

If you want to apply some special styles to these links, use the following selectors in your custom.css file:

  • .custom .page-numbers – to target all the comment page links
  • .custom .prev.page-numbers – to target the “previous” comment page link
  • .custom .next.page-numbers – to target the “next” comment page link

Note that you can change the arguments in the $args array to customize the output of the links (see the WordPress Function Reference page for the available arguments); however, you must keep the echo argument set to false, or else you’ll get unintended results!