feed me feed me chillireward

By Tag comments

Reverse Order in comments

 

Most wordpress themes (nearly all of them) display comments in an ascending order. That is the first comment is on top and the latest at the bottom of the page. If you are anything like me and want to dig into the latest comment right away, you would prefer a reverse ordering of comments. This is a preferred approach in popular blogs, as otherwise you have to literally scroll down zillions of comments, to finally reach the latest..

onetwothree

While achieving this in PHP is childs play, accessing a couple of functions in WP took some hunt and peck approach. I finally managed to locate the important functions that would get us the desired look.

So the magic functions are :

get_comments_number();


array_reverse($comments,true);

get_comments_number() returns the total number of approved comments.
array_reverse($comments,true) queries for comments and returns them in reverse chronological order. Thank you Verne for this!
All we have to do is bring the above two together, and voila, you get a numbered, reverse ordered comment system..

18th Jan 2008: Yikes! There is a mistake in the code, I gave yesterday. I incremented the comment count, which was not needed. The culprit was the $comments_count–; variable that I called before displaying the comment. I apologise to anyone who borrowed the code, in blind faith.

    The steps are:

  1. Get total comments
  2. Show comments
  3. reduce comment counter, as we are counting downwards.

Here is the code for the same with suitable comments..

<?php if ($comments) : ?>
<div class="displaycomments">
<h3 class="shdr">
     <?php comments_number(,‘My very first comment<span class="exclamation">!</span>’,‘ <span class="totalnumber">%</span> opinions <span class="exclamation">!</span> <small>(recent->oldest)</small>’);
     ?>
     </h3>

<?php
/* get the total number of comments  */
$comment_count=get_comments_number();

/* get the comments in reverse order */
$comments = array_reverse($comments, true);

/*show the comments one by one*/
foreach ($comments as $comment) :

/*this determines whether comment is direct comment or a trackback from another blog */
$comment_type = get_comment_type();
$isByAuthor = false;

/* checking to see if e-mail is authors [for different styling purpose]
the variable, $oddcomment has values ‘alt’ AND ‘def’ which help style the CSS classes for alternate look in comment display */

if(($comment->comment_author_email == ‘lmareddy@yahoo.com’)||($comment->comment_author_email == ‘lak@mareddy.com’)) {
     $isByAuthor = true;
     $oddcomment = ‘auth’;
     }
     elseif (‘alt’ == $oddcomment && $isByAuthor == false) $oddcomment = ‘def’;
     else $oddcomment = ‘alt’;

/*checking to see if the number is single digit.  If yes, add a zero before it to display stylishly.*/
if (strlen($comment_count)==1) {$comment_count = ‘0′.$comment_count;}
     ?>

<div class="comment" id="comment-<?php comment_ID() ?>">
               <span class="commentdatetime"><?php comment_date(‘d M Y’) ?><em><?php comment_time()?></em></span>

<!– this is the class that decides styling for author/alternate styles –>
<div class="<?php echo $oddcomment; ?>">

     <span class="commentnum"><?php echo $comment_count;?></span>     

<!– the following is the mygravatar part which shows photos of commentors–>             
     <div class="mygravatarwrap"><div class="mygravatars">
     <?php if(function_exists(‘autoavatar’)){ autoavatar(‘http://chilligavva.com/avatars/defavatar.png’,‘Check out my myBlogLog profile!’,‘Click and make yourself a gravatar!’);}?>
     </div></div>   
<!– end photo part –>                                

<div class="thecomment">      
     <span class="commentauthor"><em><?php comment_author_link(); ?></em> said</span>
<?php edit_comment_link(__("Edit"), ‘ ? ‘, ); ?>     
<?php comment_text() ?>
</div>

<!– end altcomment –>  </div>
<!– end comment –></div>

<?
/*lets now reduce the comment number by one, as we are counting downwards..*/
$comment_count–;
?>
<?php endforeach; /* end for each comment */ ?>
<!– end displaycomments –></div>