Display Most Recent Comments with Gravatars
Have you seen sites that display most recent comments in their sidebar with user gravatars. Well this can be done easily with these codes. Simply paste the following code anywhere you want to display the most recent comments.
01 | <?php |
02 | $query = "SELECT * from $wpdb->comments WHERE comment_approved='1' |
03 | ORDER BY comment_date DESC LIMIT 0 ,5"; |
04 | $comments = $wpdb->get_results($query); |
05 |
06 | if ($comments) { |
07 | echo '<ul>'; |
08 | foreach ($comments as $comment) { |
09 | $url = '<a href="'. get_permalink($comment->comment_post_ID).'#comment-'.$comment->comment_ID .'" title="'.$comment->comment_author .' | '.get_the_title($comment->comment_post_ID).'">'; |
10 | echo '<li>'; |
11 | echo '<div class="img">'; |
12 | echo $url; |
13 | echo get_avatar( $comment->comment_author_email, $img_w); |
14 | echo '</a></div>'; |
15 |
16 | echo '<div class="txt">Par: '; |
17 | echo $url; |
18 | echo $comment->comment_author; |
19 | echo '</a></div>'; |
20 | echo '</li>'; |
21 | } |
22 | echo '</ul>'; |
23 | } |
24 | ?> |
To get more or less than 5 comments, change the number 5 on line 2.
I see lots of tutorials about how to get the most recent comments in WordPress. Irrespective of how popular is your blog but it’s always good idea to show commenter’s gravatar in sidebar. Then what is the best way to show most recent comments on your WordPress blog with Gravatar?
Simply put below code to your theme’s file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?php
global $wpdb;
$query = "SELECT * from $wpdb->comments WHERE comment_approved= '1'
ORDER BY comment_date DESC LIMIT 0 ,10"; // This shows top 10 comments
$comments = $wpdb->get_results($query);
if ($comments) {
echo '<ul>';
foreach ($comments as $comment) {
$url = '<a href="'. get_permalink($comment->comment_post_ID).'#comment-'.$comment->comment_ID .'" title="'.$comment->comment_author .' | '.get_the_title($comment->comment_post_ID).'">';
echo '<li>';
echo '<div class="img">';
echo $url;
echo get_avatar( $comment->comment_author_email, $img_w); // Get Gravatar Image
echo '</a></div>';
echo '<div class="txt">Par: ';
echo $url;
echo $comment->comment_author;
echo '</a></div>';
echo '</li>';
}
echo '</ul>';
}
?>
|
Let me know what you think about this.
Have a suggestion or anything to add to this article? Chime in and share as a comment.
Display Most Recent Comments with Gravatars
Reviewed by Unknown
on
3:45 AM
Rating:
No comments: