0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

A Better Way to Display Recent Comments without a Plugin

Here is a better way to display your recent comments without a plugin. Instead of slapping a bunch of gnarly code into our index.php or sidebar.php file, we are going to slap it right where it belongs: in your theme’s functions.php file.

Here is the code, all exploded for your viewing and analytical pleasure (well, for mine anyway). For a more minified version of this snippet, be sure to check out the source link.

<?php // display recent comments without a plugin

function recent_comments($src_count=10, $src_length=60, $pre_HTML='<ul>', $post_HTML='</ul>') {
global $wpdb;
$sql = "
	SELECT DISTINCT 
		ID, 
		post_title, 
		post_password, 
		comment_ID, 
		comment_post_ID, 
		comment_author, 
		comment_date_gmt, 
		comment_approved, 
		comment_type, 
	SUBSTRING(
		comment_content, 1, $src_length
		) 
	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 $src_count
";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;

foreach ($comments as $comment) {

	$output .= '<li><a href="'.get_permalink($comment->ID).'#comment-'.$comment->comment_ID.'" title="on '.$comment->post_title.'">'.strip_tags($comment->com_excerpt).'...</a></li>';

}
$output .= $post_HTML;
echo $output;

} ?>

Ah, it’s a thing of beauty. Once you have that code in place, rock it out anywhere in your theme with this charming little tag:

<?php recent_comments(); ?>

see also: Display Latest Comments without a Plugin

Source: Blog Oh Blog (404 link removed 2013/06/24)




0