We recently discussed about How to: Show Top Commentators in WordPress (without plugin). Besides the possibility of the top commentators’ view in your blog, you might also list the last written comments. WordPress for this purpose already has a standard widget, and various plugins, but for those who wish on the issue may be flexible and require more design freedom, which I show here an alternative way.
You can customize any design on your blog with a little CSS.
The code to determine the last comments
The following code can be used in the functions.php file to be copied:
function get_latest_comments($limit = 5)
$results = $wpdb->get_results('
'.$wpdb->posts.' p ON (c.comment_post_id = p.ID)
comment_approved = "1"
AND c.comment_type = ""
AND p.post_status != "trash"
ORDER BY c.comment_date DESC
$output = '<ul>';
foreach ($results as $row)
$comment_url = get_comment_link($row->comment_ID);
$comment_author = $row->comment_author;
$comment_text = mb_substr($row->comment_content, 0, 50, 'utf-8').'...';
$output .= '<li><a href="'.$comment_url.'" title="Kommentar zu: ' . $row->post_title . '">';
$output .= $comment_author.': '.$comment_text;
$output .= '</a></li>';
$output .= '</ul>';
In the first part of this function (line 1-22), the last five written comments are fetched from the database. The second portion (lines 24 - 34), however, is the composition of the output. In this case, we simply create a list item for each comment <li> </ li> , which in turn contains a link that points to the appropriate article and the comment.
Brief explanation of the SQL statement in detail
SELECT (lines 6-11)
Selects the comment IDs are the names of the commentators, the comment text, the commentary accompanying the post ID (Item ID) and the title of the article.
FROM (line 12 - 15)
We refer to the comments table by WordPress and combine them using a JOIN or with the posts table. The join is done via the post id.
WHERE (line 16-19)
The terms include the following:
- the comment has been approved
- The comment is also one and not a Trackback or Pingback
- Commenting on the article is not in the Recycle Bin (Trash)
ORDER BY and LIMIT (lines 20 - 21)
The selected Parent records appear in descending order by the comment date. It continue to be define how many comments are retrieved from the database (default is 5).
The code to display the recent comments
The following code can be placed anywhere in your WordPress theme. So if you want eg the list of the last 10 comments can be displayed in the sidebar, you can add the following code example in the sidebar.php to insert.
1.echo get_latest_comments (10);
Depending on how you want to have the issue, you can take an example code of course customize.
It is also be possible additionally to indicate when the comment was posted. To do this you simply would have to pick the date from the database and integrate with the issue. In addition, you could still make a restriction, so your comments, comments from the admin, are not displayed.
A full-time blogger & Digital Marketing Expert in India. An early adopter who loves to try out various Digital, Blogging & Social media tools along with exploring some cool iOS and Android apps.