DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

How To Add Recent Comments To Wordpress Without A Plugin

11.25.2008
| 5557 views |
  • submit to reddit
        // Add the following code to the functions.php file inside your theme folder.

function dp_recent_comments($no_comments = 10, $comment_len = 150) { 
    global $wpdb; 
	
	$request = "SELECT * FROM $wpdb->comments";
	$request .= " JOIN $wpdb->posts ON ID = comment_post_ID";
	$request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''"; 
	$request .= " ORDER BY comment_date DESC LIMIT $no_comments"; 
		
	$comments = $wpdb->get_results($request);
		
	if ($comments) { 
		foreach ($comments as $comment) { 
			ob_start();
			?>
				<li>
					<a href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>"><?php echo dp_get_author($comment); ?>:</a>
					<?php echo strip_tags(substr(apply_filters('get_comment_text', $comment->comment_content), 0, $comment_len)) . "..."; ?>
				</li>
			<?php
			ob_end_flush();
		} 
	} else { 
		echo "<li>Sin comentarios</li>";
	}
}

// Usage, put this where you want to see your recents comments, the number between () is the number of comments to be showed.

dp_recent_comments(5);