Installing too many plugins will make the WordPress blog work slowly and unstable, use code built-in your theme will reduce the system resources.
I’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?
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
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
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”]<?php
global $wpdb;
$sql = "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->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = ‘1’ AND comment_type = ” AND post_password = ” ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= "n<ul>";
foreach ($comments as $comment) {
$output .= "n<li>".strip_tags($comment->comment_author) .":" . "<a href="" . get_permalink($comment->ID)."#comment-" . $comment->comment_ID . "" title="on ".$comment->post_title . "">" . strip_tags($comment->com_excerpt)."</a></li>";
}
$output .= "n</ul>";
$output .= $post_HTML;
echo $output;
?>[/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”]<ul class=">
<li>comments</li>
</ul>
[/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 .= "n<ul class="recent-comment">";[/php]
Create CSS selector:
[code lang=”css”].recent-comment ul li {
display: block;
}[/code]
very goods
Hi all
very nices post.
thank admin
wery nice blog :)
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)?
nice blog and good post
@Clouseau: It worked on this blog, you can view it in the sidebar
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!
You can use the snippet below:
[php] <?php global $wpdb;
$sql = "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->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = ‘1’ AND comment_type = ” AND post_password = ” ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= "n<ul>";
foreach ($comments as $comment) {
$output .= "n<li>".strip_tags($comment->comment_author) ." on <a href="" . get_permalink($comment->ID)."#comment-" . $comment->comment_ID . "" rel="nofollow" title="on ".$comment->post_title . "">".$comment->post_title ."</a> : " . strip_tags($comment->com_excerpt)."…</li>";}
$output .= "n</ul>";
$output .= $post_HTML;
echo $output; ?>
[/php]
Have fun!
Thank you very much, you made our day!
PS: in the code above, the fell off, but in the email notification it was there…
How do you display more content of the comments?
You can edit the valua 30 in this string to whatever you want :)
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.
The output content in HTML format, you can style the ul list as you want by CSS
Great piece of code. Thanks.
How could I modify this to call a specific post and show the comments from that post only?
good article very interesent
Hey interesting article and nice site you have
very good
Thank you very much I’ll give it a try.
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…
A beautiful and high quality information. this paper is accurate to be useful. Thanks to the author.
A beautiful and high quality information. this paper is accurate to be useful. Thanks to the author.
I think my biggest problem is too many plugins going at once.
I will be tying the code out soon.
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.
Hi. I wanted to add Avatar recognition to this recent comments code:
but no matter where I put the code:
It always doesn’t work. How can I fix the problem?
Thank you so much.
How could I modify this to call a specific post and show the comments from that post only?
I like the theme, thanks very much
for the recent comment, anyway to tweak it to show only latest comment for a specific post?
it’s a helpful info for blogger like me…
thank you for your info :)
I think my biggest problem is too many plugins going at once.
I will be tying the code out soon.