Forums

How do I reverse the comments list? (10 posts)

  1. siruanthi
    Member
    Posted 3 years ago #

    Hello, I have a page with comments on my blog. It shows the first comment first numbered as 1, 2, 3 and so on. Is there a way or a pluggin that reverses this list, so the last comment is listed first, like 3, 2, 1 ?
    Thanks.

  2. skippy
    Member
    Posted 3 years ago #

    I'm not aware of any such plugin; but you could probably do it quickly enough.

    In your theme's comments.php file, find the line that reads:
    <?php foreach ($comments as $comment) : ?>

    Immediately above that, please this:
    <?php $comments = array_reverse($comments, true); ?>

    Note: this is untested. If it works, please let us know!

  3. siruanthi
    Member
    Posted 3 years ago #

    Thanx very much skippy. It works fine!!! The newest/latest comment shows up first. Only thing is...is it possible to reverse the comment numbers too, because they haven't reversed.

  4. siruanthi
    Member
    Posted 3 years ago #

    I have a line of code in the comments-template like this:

    <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

    should i add such a line as you said here too ?

  5. skippy
    Member
    Posted 3 years ago #

    No. You only need the array_reverse() function immediately before the foreach ... loop.

    Try changing true to false on the array_reverse() line to swap the modify the comment numbers.

  6. siruanthi
    Member
    Posted 3 years ago #

    I changed "true" to "false" . The comments are still reversed but the numbers are still the same.... Nothing happened ..

  7. skippy
    Member
    Posted 3 years ago #

    Oh; right. The problem is that the comment list uses an HTML ordered list to construct the numbers:
    <ol class="commentlist">
    <?php foreach ($comments as $comment) : ?>
    <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">

    If the comment numbers are really important to you, you could suppress the digits for the ordered list; then do something like this:
    $count = count($comments);
    <ol class="commentlist">
    <?php foreach ($comments as $comment) : ?>
    <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
    <?php echo $count; ?>
    // the rest of the comment loop here...
    $count--;
    <?php endforeach; /* end for each comment */ ?>
    </ol>

  8. siruanthi
    Member
    Posted 3 years ago #

    Hmmz, that second code didn't quite work, "$count--;" showed up between every post. I checked and double-checked if i made an error/mistake.. but numbers aren't that important to me... so i stick to your first line of code, ....it's not exactly what i wanted but close enough, thanx very much for your help effort and your patience with me :-)

  9. skippy
    Member
    Posted 3 years ago #

    Sorry, that line should have read:
    <?php $count--; ?>

  10. Truth
    Member
    Posted 2 years ago #

    I tried this and it works but I have to change

    $count = count($comments);

    to

    <?php $count = count($comments); ?>

    or put $count = count($comments);

    at the top inside the <?php and ?>

    for it to work.. thanks :)

Topic Closed

This topic has been closed to new replies.

About this Topic