Get the comments

OK: the first WordPress hack, the last 7 comments on your blog (or whatever number you’d like). I like the way this is on Alan’s page where it’s called ‘Active conversations’. On Alan’s page the whole thread is lifted out (which is particularly neat too), in my case I only wanted to see the comments (and their permalinks).

<?php if ( $comments = $wpdb->get_results(“SELECT comment_author, ” .
“comment_author_url, comment_ID, comment_post_ID “.
“FROM $wpdb->comments WHERE comment_approved = ‘1’ ” .
“ORDER BY comment_date_gmt DESC LIMIT 5”) ){
?>
<li>
<h2><?php _e(‘Comments’); ?></h2>
<ul>
<?php foreach ($comments as $comment) {
echo ‘<li>’ .
sprintf(‘%s on %s’, $comment->comment_author,
‘<a href=”‘. get_permalink($comment->comment_post_ID) .
‘#comment-‘ . $comment->comment_ID . ‘”>’ .
get_the_title($comment->comment_post_ID) . ‘</a>’);
echo ‘</li>’; }
}
?>

</ul></li>

Paste the code in your theme’s sidebar.php file (somewhere between line 44 and line 50).

Some observations: First of all, I like to see a date. Maybe I should group the data first and then print (echo) it off. Secondly, look at the get_the_title! It gets an ID as a parameter which means that there’s going to be another query fired off on the server. Not really efficient (note, this code you can also find [minus the small change] in your WordPress dashboard) . Tell the boys that.

More after.

ed: I went through great lengths getting the code to show up nice. Watch out for extra spaces when you copy the snippet. Also, if you have a spare *nux computer to play with, I highly recommend you to test it on that machine first.

This entry was posted in Ordinateurs, Wordpress. Bookmark the permalink.

10 Responses to Get the comments

  1. alfons says:

    Perhaps better to choose to use a text area here?

  2. Arthur says:

    Perhaps better to choose to use a text area here?

    For the quoted text? Might be a better idea yes.

  3. alfons says:

    It’s pretty cool though. Now I need also a list with sorted comments, including updated ones. (I’m used to update entries, add corrections or new information.)

  4. Arthur says:

    It’s pretty cool though. Now I need also a list with sorted comments, including updated ones. (I’m used to update entries, add corrections or new information.)

    As in revision, you mean? On a separate page?

  5. alfons says:

    Oh it really sucks putting the code that way in your page. WordPress messes up apostrophes and quote characters.

  6. alfons says:

    What I did to get the sample was just retrieving your sidebar.php…
    I noticed the file had some weird indenting, probably tried to paste it into a SSH terminal’s vi session? :-)

  7. Arthur says:

    I noticed the file had some weird indenting, probably tried to paste it into a SSH terminal’s vi session? :-)

    No. Your vi sucks. My Debian vi supports DEL and BKSPC appropriately. Yours sucks.

  8. Arthur says:

    My Debian vi supports DEL and BKSPC appropriately. Yours sucks.

    On preview, I may run an older vi though… It’s Debian…

    (Vi IMproved 6.2 (2003 Jun 1, compiled Apr 1 2004 23:32:52))

  9. alfons says:

    I never use BACKSPACE. Del works fine for me, and I’m pretty sure for you too, because your .vimrc is a copy of mine. (Which is a barebones one!)
    Did you go to edit mode first? (HAHAHAHAHAHAHA!)
    By the way, I corrected your glorious chunk of PHP to check for pingbacks from myself, by changing this line:

    “FROM $wpdb->comments WHERE (comment_approved = ‘1’ AND (comment_type != ‘pingback’ OR comment_author_IP != ‘80.60.233.195’))” .

  10. alfons says:

    Oh, if you need your backspace, put the following in your .vimrc:

    set backspace=indent,eol,start ” backspace for dummysQuoted from here.

Comments are closed.