Actually, I did not create a plugin but I wrote a code snippet that did this job for me.
Add these lines to comments.php in your active theme folder.
$comments_per_page = 5;
global $post;
$total = $post->comment_count;
if($total > 0) {
$apage=isset($_GET['apage']) ? intval($_GET['apage']) : 1;
$comments=array_slice($comments, intval(($apage-1)*$comments_per_page),intval($comments_per_page));
}
Insert this code right after if ($comments) statement
$commentCounter = (($apage-1)*$comments_per_page);
$from_page = $commentCounter+1;
if($apage == 1) {
$to_page = $comments_per_page;
if($total < $to_page) {
$to_page = $total;
}
}
else {
$to_page = $from_page + $comments_per_page - 1;
if($to_page > $total) {
$to_page = $total;
}
}
Insert this line where you want page links to appear in comments.php.
<?php echo paginate_links( array(
'base' => add_query_arg( 'apage', '%#%#comments' ),
'format' => '',
'total' => ceil($total / intval($comments_per_page)),
'current' => $apage
));
I think this is it. It should work. Give it a try and share your feedback via comments.
It did not work. Tried on WordPress 2.5.1.
Rafael Dourado, can you please tell me what is actually happening?
good catch!
replace this line
'total' => ceil($total / $num),with this,
'total' => ceil($total / intval($comments_per_page)),I have changed it in my code so give it a try now.
Now this warning appeared:
Warning: Division by zero in [...]\wp-content\themes\theme\functions.php on line 28
The problem is with the new line.
Before the change it was just not working. Nothing happened.
And when I change the variable to a number, it still do not work (but the warning do not shows up).
change $num or whatever number you just put in your code here (functions.php on line 28) with
intval($comments_per_page)So line 28 in your functions.php should look like this,
'total' => ceil($total / intval($comments_per_page)),It should work. I just tried and it is working with my theme.
Now it is working fine! Thank you for being so helpful. I prefer using this solution than the plugins available. They are all too complicated to implement.
Thank you.
Hi, I found your blog on this new directory of WordPress Blogs at blackhatbootcamp.com/listofwordpressblogs. I dont know how your blog came up, must have been a typo, i duno. Anyways, I just clicked it and here I am. Your blog looks good. Have a nice day. James.
Cheers mate! Just saying cheers and thanks for the code. Whenever possible, I prefer to script functionality into my themes instead of using plugins, as plugins tend to have too much code to rummage through in order to make even the simplest changes. Rock on!
I am building my first template and would like to know how to customize the format for this? I don’t see anything that call the ‘next’ or ‘previous’ or even the numbers in the pagination. Any help would be appreciated!
Thanks in advance,
J. D. Payne
Also is there a way to have the most recent comments first? In other words in “descending” order rather than the oldest post on the first page of comments?
Thanks again, this is wonderful NOT to have to use a plugin!
J
Also is there a way to reverse the order, newest first?
Jonathan, you can try http://php.net/array_reverse
Can you please explain what exactly this bug fixes?
I am having an issue that might be related to this. I have comments per page set to 5 in the admin panel. When a post has 6 comments, it will display 1 comment on the first page, and then 5 comments when you click “Older Comments.” I basically want to reverse this behavior, with 5 comments showing up first, then when you click “Older Comments”, it shows 1 comment. If you could please post here or email me, I would appreciate it!
I was searching for something like this since 2 days. I am actually converting my html site into wordpress and my html site comments had some extra fields and different pagination requirements. And your code has helped me achieve that! Thanks a Lot!
Could it be that the affects of Food Inc. ,
Thank you!!
@Jeff,
$comments = array_reverse($comments, true);
$comments = array_slice($comments, intval(($apage-1)*$comments_per_page), intval($comments_per_page));
@Jeff, this solution works with earlier versions of wordpress not with the one came out with comments pagination. You can get rid of wordpress core pagination in the template, if there is any, and try only this one. This should work stand alone in your template comments.php file.