Last comment page first with full number of comments?

I use paged and threaded comments with:

  • 10 comments per page
  • the last comment page first
  • the newest comments first

If there are 21 comments total, the first page only contain 1 comment – the newest. How do i get the newest 10 comments always showing on the first page?

I’m using WordPress 3.0.3.
Edit: Upgraded to 3.1, the problem still exists…

Edit: Forgot to add the code I’m using. I use a callback for the listed comments, here goes:

function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
    <div id="comment-<?php comment_ID(); ?>" class="comment">

        <?php if(function_exists('hkTC_comment_title'))
            hkTC_comment_title($comment->comment_ID,'<strong class="comment-title">','</strong>');
        ?>

        <?php comment_text() ?>

        <span class="written-by">
            Skrivet av:
            <?php
            $commentAuthID = get_comment(get_comment_ID())->user_id;
            echo the_author_meta('first_name',$commentAuthID)." ";
            echo the_author_meta('last_name',$commentAuthID);
            ?>
            <em>kl. <?php echo get_comment_time('H.i'); ?>, den <?php echo get_comment_date('j F Y'); ?></em>
        </span>

        <div class="reply">
            <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
        </div>
    </div>
    <?php }

The hkTC_comment_title function is from this plugin that I’m also using:
http://wordpress.org/extend/plugins/hikari-title-comments/
But that shouldn’t mess with the number of comments I think.

Edit: This is my custom template used to display the page with the comments:
http://123.writeboard.com/4x2m38ifvnkrct7l

1 Answer
1

This is indeed the default behavior of WordPress. The comments always stay on the same comment page, so if you show the last comment page first, that last comment page will not always have the “full” number of comments. The advantage of keeping a comment always on the same comment page is that the URL always stays the same, otherwise a comment can move to another page when new comments are added.

But it’s annoying, and I needed a workaround for this too. I found no (easy?) way to do this with WordPress hooks, so I sliced the comment array myself, by reading the cpage (comment page) query variable. Watch out with this variable, because WordPress will be so helpful to reverse it for you when you set the “first/last comment page first” to “last”.

This code goes in the comments.php theme file, because it reads the global variable $comments. I did not use a comment callback (it was very simple code), but you can still pass the newly sliced $comments_to_display array to wp_list_comments() as the second argument, it will use that instead of all comments then. I don’t know how this will work with threaded comments (how do they work with “normal” paged comments?).

$comments_to_display = $comments;

/*
 * Display the comments over pages, with the newest comments on the first page
 * Special in this code (not in WP by default) is that the first page will contain 5 comments,
 * and the final page can contain less comments.
 */
$comments_per_page = 5; // MAYBE: use get_option()?
$comment_page = get_query_var( 'cpage' );
$comment_this_page_start = 0;
$comment_this_page_count = $comments_per_page;
$oldest_comment_page_count = count( $comments_to_display ) % $comments_per_page;
if ( 0 == $oldest_comment_page_count ) {
    $oldest_comment_page_count = $comments_per_page;
}
if ( 1 == $comment_page ) {
    $comment_this_page_count = $oldest_comment_page_count;
} else {
    $comment_this_page_start = $oldest_comment_page_count + ($comment_page - 2) * $comments_per_page;
}
$comments_to_display = array_slice( $comments_to_display, $comment_this_page_start, $comment_this_page_count );
// You probably want to array_reverse() $comments_to_display, currently it shows the oldest on top.
// Then you should be able to pass $comments_to_display to wp_list_comments() and it will use your shorter array instead.

Leave a Comment