How to list most recent comments and recent post in WordPress

Installing too many plugins will make the WordPress blog work slowly and unstable, use code built-in your theme will reduce the system resources.

Wordpress recent posts and recent commentsI’ve known a ton of plugins and widgets that allows me to list recent comments and recent post on my WordPress blog but I’m not using it. Because I’ve integrated some useful code below then I get results similar to those extensions which I’ve known.

List most recent comments


If you want change the number comments will be display, simply change the “10” in the $sql variable.

List most recent posts

Do you want to deactivate your List most recent comments and recent post plugins then use code snippets above?

wordpress recipes

36 Comments

  1. Hi,

    I cannot get this to work. “<a href="" = syntax error for me in Aptana Studio and I get the same result if I ignore that and try anyway :)

    Can you help? Thanks

    • I recommend you using another code editor just as Notepad++, Notepad2 … or you can try notepad to edit that code

  2. Hello:

    Thanks for sharing the code for listing of comments. I have used it at indicpost.com/readers-zone/. You can see it after clicking the “Recent Comments” tab.

    However, as you can see, the comments are NOT in the desired output. I want JUST ONE COMMENT PER LINE. Please guide me on the subject. Just for info, I am a journalist and not a techie.

    Thanks in advance,

    Best Regards,
    Anshuman (from INDIA)

    • Because the output is unordered list, so you can style it by using CSS in your html code.
      You can use

      display: block;
  3. Hello:

    Thanks a lot for your reply.

    At the risk of antagonising you, I’m afraid I will have to ask you the basic question:

    Where do I put “display: block;” in the following:

    —————————————————–

    Note: Click the name of the user to reach the exact news story where the comment was posted.

    Recent Comments
    [php htmlscript=”true”]&lt;?php
    global $wpdb;
    $sql = &quot;SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb-&gt;comments LEFT OUTER JOIN $wpdb-&gt;posts ON ($wpdb-&gt;comments.comment_post_ID = $wpdb-&gt;posts.ID) WHERE comment_approved = ‘1’ AND comment_type = ” AND post_password = ” ORDER BY comment_date_gmt DESC LIMIT 10&quot;;

    $comments = $wpdb-&gt;get_results($sql);
    $output = $pre_HTML;
    $output .= &quot;n&lt;ul&gt;&quot;;
    foreach ($comments as $comment) {
    $output .= &quot;n&lt;li&gt;&quot;.strip_tags($comment-&gt;comment_author) .&quot;:&quot; . &quot;&lt;a href=&quot;&quot; . get_permalink($comment-&gt;ID).&quot;#comment-&quot; . $comment-&gt;comment_ID . &quot;&quot; title=&quot;on &quot;.$comment-&gt;post_title . &quot;&quot;&gt;&quot; . strip_tags($comment-&gt;com_excerpt).&quot;&lt;/a&gt;&lt;/li&gt;&quot;;
    }
    $output .= &quot;n&lt;/ul&gt;&quot;;
    $output .= $post_HTML;
    echo $output;
    ?&gt;[/php]

    —————————————————–

    I am sorry but I am only a journalist and am learning the thing through trial and error only.

    Thanking you in advance,

    Best Regards,
    Anshuman

    • You will get the HTML output is:
      [code lang=”xml”]&lt;ul class=&quot;&gt;
      &lt;li&gt;comments&lt;/li&gt;
      &lt;/ul&gt;
      [/code]
      Everythings you need to do is create a CSS selector for that list.

      Change the line 7th to: [php htmlscript=”true” light=”true”]$output .= &quot;n&lt;ul class=&quot;recent-comment&quot;&gt;&quot;;[/php]
      Create CSS selector:
      [code lang=”css”].recent-comment ul li {
      display: block;
      }[/code]

  4. very goods

  5. wery nice blog :)

  6. I used this ‘recent comments’ function: http://www.blogohblog.com/10-wordpress-hacks-to-make-your-life-even-easier/

    it’s the same code, which I have seen on several blogs, but nobody mentions/notices that the output is not working…
    i.e.: it doesn’t display the author nor post-title – only the excerpt?

    what I need is the following: Author on: title – excerpt (title being the link to the post) – how to edit this line:

    $output .= “n”.strip_tags($comment->comment_author) .”:” . “ID).”#comment-” . $comment->comment_ID . “” title=”on “.$comment->post_title . “”>” . strip_tags($comment->com_excerpt).”“;

    to get the desired result (as I’m no php coder)?

  7. @Clouseau: It worked on this blog, you can view it in the sidebar

  8. Hi Narga – you’re right: title is shown as ‘alt-tag’ – it is working here in your sidebar – not sure why it didn’t work on mine….

    Could you help me updating that code/line? I want it to look like:

    “Narga on: how-to-list-most… – ‘It worked on this blog’…”

    Thanks in advance!

  9. You can use the snippet below:
    [php] &lt;?php global $wpdb;
    $sql = &quot;SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb-&gt;comments LEFT OUTER JOIN $wpdb-&gt;posts ON ($wpdb-&gt;comments.comment_post_ID = $wpdb-&gt;posts.ID) WHERE comment_approved = ‘1’ AND comment_type = ” AND post_password = ” ORDER BY comment_date_gmt DESC LIMIT 10&quot;;
    $comments = $wpdb-&gt;get_results($sql);
    $output = $pre_HTML;
    $output .= &quot;n&lt;ul&gt;&quot;;
    foreach ($comments as $comment) {
    $output .= &quot;n&lt;li&gt;&quot;.strip_tags($comment-&gt;comment_author) .&quot; on &lt;a href=&quot;&quot; . get_permalink($comment-&gt;ID).&quot;#comment-&quot; . $comment-&gt;comment_ID . &quot;&quot; rel=&quot;nofollow&quot; title=&quot;on &quot;.$comment-&gt;post_title . &quot;&quot;&gt;&quot;.$comment-&gt;post_title .&quot;&lt;/a&gt; : &quot; . strip_tags($comment-&gt;com_excerpt).&quot;…&lt;/li&gt;&quot;;}
    $output .= &quot;n&lt;/ul&gt;&quot;;
    $output .= $post_HTML;
    echo $output; ?&gt;
    [/php]
    Have fun!

  10. Thank you very much, you made our day!

    PS: in the code above, the fell off, but in the email notification it was there…

  11. How do you display more content of the comments?

  12. You can edit the valua 30 in this string to whatever you want :)

    SUBSTRING(comment_content,1,30)
  13. However, as you can see, the comments are NOT in the desired output. I want JUST ONE COMMENT PER LINE. Please guide me on the subject. Just for info, I am a journalist and not a techie.

  14. The output content in HTML format, you can style the ul list as you want by CSS

  15. Great piece of code. Thanks.

    How could I modify this to call a specific post and show the comments from that post only?

  16. good article very interesent

  17. Hey interesting article and nice site you have

  18. Thank you very much I’ll give it a try.

  19. Is there any reason why use the code instead of the plugin that allows you to show recent comments?

    • So many reasons:

      Style your recent comments as you want
      Combine css and javascript in to your theme without include some unused code
      It always work on your theme, if you using plugins, may be it incompatible with new or old version of your theme.
      more and more…

  20. A beautiful and high quality information. this paper is accurate to be useful. Thanks to the author.

  21. A beautiful and high quality information. this paper is accurate to be useful. Thanks to the author.

  22. I think my biggest problem is too many plugins going at once.
    I will be tying the code out soon.

  23. This blog appears to recieve a great deal of visitors. How do you promote it? It offers a nice unique twist on things. I guess having something authentic or substantial to talk about is the most important factor.

  24. Hi. I wanted to add Avatar recognition to this recent comments code:

    comments LEFT OUTER JOIN $wpdb-&amp;gt;posts ON ($wpdb-&amp;gt;comments.comment_post_ID = $wpdb-&amp;gt;posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10&quot;;
    $comments = $wpdb-&amp;gt;get_results($sql);
    $output = $pre_HTML;
    $output .= &quot;n&quot;;
    foreach ($comments as $comment) {
    $output .= &quot;n&quot;.strip_tags($comment-&amp;gt;comment_author) .&quot; on &lt;a href=&quot;&quot; rel=&quot;nofollow&quot;&gt;ID).&quot;#comment-&quot; . $comment-&amp;gt;comment_ID . &quot;&quot; rel=&quot;nofollow&quot; title=&quot;on &quot;.$comment-&amp;gt;post_title . &quot;&quot;&amp;gt;&quot;.$comment-&amp;gt;post_title .&quot;&lt;/a&gt; : &quot; . strip_tags($comment-&amp;gt;com_excerpt).&quot;...&quot;;}
    $output .= &quot;n&quot;;
    $output .= $post_HTML;
    echo $output; ?&amp;gt;
    

    but no matter where I put the code:

    comment_author_email, '55' ); ?&amp;gt;

    It always doesn’t work. How can I fix the problem?
    Thank you so much.

  25. How could I modify this to call a specific post and show the comments from that post only?

  26. I like the theme, thanks very much

  27. for the recent comment, anyway to tweak it to show only latest comment for a specific post?

  28. it’s a helpful info for blogger like me…
    thank you for your info :)

  29. I think my biggest problem is too many plugins going at once.
    I will be tying the code out soon.


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *
Name *
Email *
Website

This site uses Akismet to reduce spam. Learn how your comment data is processed.