Support » Fixing WordPress » flip comment order

  • Resolved troglodyth


    Hi folks, usually (for discussion) it’s perfect having the oldest comment on top and the latest comment at the bottom, but i decided to make one wp-page a guestbook. In online guestbooks the reversed order would fit better, thus the latest comment is on top. My question: Is it possible to change the comment order for one single wp-page and if so what do i have to do?

    (ps.: I have already tried the guestbook-plug-in, but it’s a waste, because it’s just setting up a wp-page titled “guestbook” automatically without changing comment order.)

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Add this to your theme’s functions.php file:

    function reverse_comments($comments,$id)
    if ($id == 42) {
    $comments = array_reverse($comments);
    return $comments;

    Replace the “42” with the ID number of the page you want the comments to be reversed for.

    Wow, this one works great. Thanks a lot. But there’s still one thing i’d like to improve. My theme gives comment counts to each comment, so the one at top is still marked “1” now, even though it is the last/latest comment (in my case 12). Can i change that, too?

    The code to the count in my themes comments.php is
    <div class="commentnumber"><?php echo $commentcount++;?></div>

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Put this somewhere before the comment loop:
    <?php if ($id == 42) $commentcount = count($comments); ?>

    Then do something like this:

    <div class="commentnumber"><?php
    if ($id == 42) echo $commentcount--;
    else echo $commentcount++;

    Where exactly do i find “the comment loop” or where does it start. I have read “The Loop” in the codex, but still have no clue.
    Anyways i’ve tried to put the first line into my templates pages.php (guestbook is a page, not a post) and the following lines to the comments.php to where i’ve found $commentcount++, it didn’t work. Tried to put first line into comment.php, too it didn’t work. Now i’ve commented out the first line and it works, but not the right way. Now every comment on my guestbook page is number 12 and comments in posts have even numbers 2, 4, 6 – very strange.
    Where did i fail?

    Found the bug – my commented out code must somehow have been executed without being written. So now i’ve put this one into comment.php before the comment list is generated

    <?php if ($id == 42) { $commentcount = count($comments); $direction = -1; }
    		else { $commentcount=1; $direction=1; } ?>

    Then i’ve substituted the div class=commentnumber line with:
    <div class="commentnumber"><?php echo $commentcount; $commentcount=$commentcount+$direction; ?></div>

    Now it works and i’m happy. Thanks for your help.

    How to include more ID’s? Do I have to repeat that code for every ID or is there a easier way to do it?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    You can do multiple ID’s like this:

    if ($id == 42 || $id == 69 || $id == 37...

    Or if you have a whole lot of ID’s to check, you can do this:

    if (in_array($id, array(42,69,37))) ...

    Thanks Otto!



    How can you flip the comment order for all of the posts? I want to get all the comments displaying the opposite way for actual posts made and not just pages. I have searched the forms and codex to no avail.

    Thank you for your help fellas!

    Encrypt, there is a plugin in the extend section. I forgot the name, just search for it.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘flip comment order’ is closed to new replies.