WordPress: Comment Pagination

If you receive large numbers of comments on your website, and you’re using WordPress, you might want to paginate your comments so that pages load faster and your visitors won’t have a hard time trying to comment.

For this tutorial you will need to be logged into your WordPress dashboard, or have access to FTP. Please note that there are many ways to do this tutorial depending on your knowledge of WordPress.

Downloading and Installing

Method one: In your WordPress dashboard: Go to Plugins, Add New and search for “Paginated Comments” (by James Maurer). Click “Install” and “Install now”.

Method two: Doing it manually: Click here to download the package.
Extract the contents of the package to your computer.
Upload the folder “paginated-comments” to /wp-content/plugins – your plugins folder.

Next, activate the plugin by going to the installed plugins in your WordPress dashboard.

Note: You can stop here, and leave the plugin as it is – view your website and see how the template looks, and go to Settings, Paginated Comments to edit the settings. If you don’t like the look of the comment pagination you can edit it by continuing with this tutorial.

Go to wp-content/plugins/paginated-comments/themes/default in your FTP. This file is the paginated comments file. Open it.

Simply compare it to your comments.php file and edit accordingly, then save the file. There should be variables that are familiar to you, and you can edit these to your liking.

Step-by-step Installation

Please note that when following this procedure, it may not be accurate as all themes are different and the files may vary.

Open the comments.php file that is located in your theme folder. You can also access this from Appearance, Editor and select “comments.php”.

Copy all this text into a new document and name it paginated-comments.php.

Replace the following text at the beginning:

<?php // Do not delete these lines
	if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
		die ('Please do not load this page directly. Thanks!');
	if ( post_password_required() ) { ?>
			

With the following:

<?php
/**
 * @package Paginated_Comments
 * @subpackage Default_Theme
 */
	// Do not delete these lines
	if (!empty($_SERVER['SCRIPT_FILENAME']) && 'paginated-comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
		die ('Please do not load this page directly. Thanks!');
	if ( post_password_required() ) { ?>
			

In this document, find the code that shows the number of comments on your blog, which should look something like this:

<?php comments_number('No Comments', '1 Comment', '% Comments' );?> on <?php the_title(); ?>
			

After that, place the following code, which displays the navigation for the pages of the comments.

<!-- Start Paginated Comments Pages -->
	<?php if ( Paginated_Comments_have_pages() ) : ?>
	<p class="paginated-comments-pages">
<?php _e("Pages:"); ?>
<?php Paginated_Comments_print_pages(); ?></p>
	<?php endif; ?>
<!-- End Paginated Comments Pages -->
			

Now find the following code:

<?php if ( have_comments() ) : ?>

Replace with:

<?php if ( $comments ) : ?>

Now, the following goes where you want the comment number to appear. You can edit the div; it doesn’t have to be exactly like this.

<div class="paginated-comments-number" style="float: left; color: #999; width: 30px; text-align: left;"&gt
<?php Paginated_Comments_numeration(); ?>
</div>

Basically, the code in bold is what displays the number. Now find the following:

<?php comment_ID() ?>

And replace it with the following:

<?php echo Paginated_Comments_URL('comment-' . get_comment_ID()); ?>

After that, save the file, and upload it to wp-content/plugins/paginated-comments/themes/default as “paginated-comments.php”.

Customisation

Go to Settings, Paginated Comments. You can edit the plugin settings here. You can choose whether you want both posts and pages to have the effect of the plugin, or just one of them.

Descending means that newer comments are displayed first.
Ascending means that newer comments are displayed last.

You can also choose the number of comments per page. All the other settings aren’t as important and don’t need to be changed.