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

[gist id=2886076]
If you want change the number comments will be display, simply change the “10” in the $sql variable.

List most recent posts

[gist id=2886081]

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

36 thoughts on “How to list most recent comments and recent post in WordPress”

  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

  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)

  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. 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)?

  5. 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!

  6. 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!

  7. 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.

    • 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…

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

  9. 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.

  10. 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.

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

Comments are closed.