Title: Last (n) comments&#8230;
Last modified: August 18, 2016

---

# Last (n) comments…

 *  Anonymous
 * [22 years, 8 months ago](https://wordpress.org/support/topic/last-n-comments/)
 * Here is some code to display a link to the last (n) commented posts:
    <?php $
   query = “SELECT ID, post_title, comment_author, comment_date FROM $tableposts,
   $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID ORDER BY $
   tablecomments.comment_date DESC LIMIT 5”; $result = mysql_query($query); while(
   $data = mysql_fetch_row($result)) { echo “$data[2]$data[1] \n”; } ?> If you changed
   your tables and database in anyway you would obviously need to edit the sql calls
   accordingly, but other than that this works just fine.

Viewing 12 replies - 1 through 12 (of 12 total)

 *  Thread Starter Anonymous
 * [22 years, 8 months ago](https://wordpress.org/support/topic/last-n-comments/#post-23937)
 * Oh I forgot to tell you what to do with the above code, simply insert this wherever
   you want the data to show up, and BAM! There it is.
    I am using thi son my site
   now, [http://www.chrisjdavis.org/](http://www.chrisjdavis.org/) it listed under,
   get this; recently commented! Later
 *  [hatespitlove](https://wordpress.org/support/users/hatespitlove/)
 * (@hatespitlove)
 * [22 years, 6 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24108)
 * Great feature. Can you explain what character “&#92” stands for or should that
   literally be put into the code? Put into my page this way just displays the title
   of my post with an /n after it and no hyperlinking.
 *  [mamabean](https://wordpress.org/support/users/mamabean/)
 * (@mamabean)
 * [22 years, 6 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24109)
 * It’s a slash. \ I had to post it to my blog to figure it out. LOL
 *  [mamabean](https://wordpress.org/support/users/mamabean/)
 * (@mamabean)
 * [22 years, 6 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24110)
 * Argh… a forward slash that is. 😉
    I can’t seem to get it to work though. I am
   assuming the code isn’t being displayed properly here. I viewed the source and
   copied but still no luck.
 *  [kenko](https://wordpress.org/support/users/kenko/)
 * (@kenko)
 * [22 years, 5 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24146)
 * replace the echo line with this
    echo "$data[2][$data[1]](https://wordpress.org/support/topic/last-n-comments/$siteurl/$blogfilename?p=$data[0])
   <br />";
 *  [mamabean](https://wordpress.org/support/users/mamabean/)
 * (@mamabean)
 * [22 years, 5 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24147)
 * That worked, thanks!!
 *  Thread Starter Anonymous
 * [22 years, 4 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24176)
 * Is there a WordPress 1.01 version of this hack?
 *  [codergurl](https://wordpress.org/support/users/codergurl/)
 * (@codergurl)
 * [22 years, 4 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24179)
 * *crosses fingers*
    `function recent_comments($limit = 5) { global $wpdb, $tableposts,
   $tablecomments, $siteurl, $blogfilename; $out = ''; $sql = "SELECT ID, post_title,
   comment_ID, comment_author, comment_date, comment_content FROM $tableposts, $
   tablecomments"; $sql .= " WHERE $tableposts.ID=$tablecomments.comment_post_ID";
   $sql .= " ORDER BY $tablecomments.comment_date DESC LIMIT $limit"; $posts = $
   wpdb->get_results($sql); foreach ($posts as $post) { $permalink = $siteurl.'/'.
   $blogfilename.'/?p='.$post->ID.'&c=1#comment-'.$post->comment_ID; $comment = 
   substr(stripslashes($post->comment_content), 0, 50); $out .= '
    - ['.$post->comment_author.'](https://wordpress.org/support/topic/last-n-comments/&apos;.$permalink.&apos;?output_format=md)-'.
      $comment.'
    - ';
       } echo $out; }
 *  Moderator [Matt Mullenweg](https://wordpress.org/support/users/matt/)
 * (@matt)
 * [22 years, 4 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24180)
 * PLEASE post code to the wiki and LINK TO IT from here. Thank you.
 *  Thread Starter Anonymous
 * [22 years, 4 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24182)
 * I would use this function for the creation of a url:
    get_permalink($recentcomment-
   >post_id) Can you put that in the script?
 *  [Chris J. Davis](https://wordpress.org/support/users/chrisjdavis/)
 * (@chrisjdavis)
 * [22 years, 4 months ago](https://wordpress.org/support/topic/last-n-comments/#post-24183)
 * This hack is now officially dead, go here: [http://www.chrisjdavis.org/index?p=439](http://www.chrisjdavis.org/index?p=439)
   for the last release.
    Author_list is dead as well, as is the copyright hack,
   at least as far as releasing them to the general public. You will still be able
   to find the code [@http](https://wordpress.org/support/users/http/)://www/chrisjdavis.
   org but I won’t be releasing them here anymore. I might announce when something
   has reached a decent level of maturity, but that is about it.
 *  [st3fo](https://wordpress.org/support/users/st3fo/)
 * (@st3fo)
 * [21 years, 1 month ago](https://wordpress.org/support/topic/last-n-comments/#post-24337)
 * This is the right code
    ` <li id="last_comments"> <?php _e('Last Comments:');?
   > <ul> <?php  $query = "SELECT ID, comment_post_ID, comment_author, comment_content
   FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID
   ORDER BY $tablecomments.comment_date DESC LIMIT 5"; $result = mysql_query($query);
   while ($data = mysql_fetch_row($result)) { if (!$data[2]) { $data[2]="Anonymous";}
   echo "<li>"; echo "[$data[2]] <a href="index.php?p=$data[1]#comments">"; echo
   substr($data[3],'0','80'); echo "...</a></li>"; } ?> </ul> </li>

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Last (n) comments…’ is closed to new replies.

 * 12 replies
 * 8 participants
 * Last reply from: [st3fo](https://wordpress.org/support/users/st3fo/)
 * Last activity: [21 years, 1 month ago](https://wordpress.org/support/topic/last-n-comments/#post-24337)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
