WordPress非插件侧栏带头像最新评论:在主题模板的侧边栏模板sidebar.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_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,25) 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 comment_author != 'Admin' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output .= "\n<li>".get_avatar(get_comment_author_email('comment_author_email'), 18). " <a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"" . $comment->post_title . " 上的评论\">". strip_tags($comment->comment_author) .": ". strip_tags($comment->com_excerpt) ."</a></li>";
}
$output .= $post_HTML;
$output = convert_smilies($output);
echo $output;
?>
其中的“Admin”换成自己的博主作者名,主要是为了限制输出博主的最新评论。然后保存文件即可。其中的CSS样式需要自己定义。也可以使用最新评论插件WP-RecentComments可以很容易地实现WordPress侧边栏最新评论带avatar头像效果