Để bạn đọc dễ dàng tham khảo những bài viết được thảo luận sôi nổi trong tuần, một số blog như eblogviet, hientinhblog, hutruc có đặt widget những bài viết có nhiều phản hồi nhất trong tuần. Bác Tính cũng có bài hướng dẫn sử dụng plugin Most Commented Widget để thực hiện. Hôm nay HT sẽ hướng dẫn các bạn tự tạo một widget mà không phải dùng đến plugin.

HT Top Post Widget

HT Top Post Widget

Bạn chỉ việc thêm đoạn code sau vào file functions.php (custom_functions.php nếu bạn đang dùng thesis)

/***** Top trong tuan http://hutruc.com *****/
function htr_top_posts($num = 10,$date=7) {
	global $wpdb;
	$num = (int) $num;
	$date = (int) $date;
	if ( $num ) {
		$limit = "LIMIT $num";
	}
	$sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) AND DATE_SUB(CURDATE(), INTERVAL $date DAY) < post_date ORDER BY comment_count DESC $limit";
	$result = $wpdb->get_results($sql, ARRAY_A);
	return $result ? $result : array();
}
function widget_htr_top_post_init() {
	if (!function_exists('register_sidebar_widget')) {
		return;
	}

	function widget_htr_top_post($args) {
		extract($args);
		$options = get_option('widget_htr_top_post');
		echo $before_widget;
		echo $before_title;
		echo $options['htr_top_post_title'];
		echo $after_title;
		echo '<ul class="htr_top_post">';
		htr_top_posts2($options['htr_top_post_num'],$options['htr_top_post_date']);
		echo '</ul>';
		echo $after_widget;

	}

	function widget_htr_top_post_options() {
		$options = get_option('widget_htr_top_post');
		if (!is_array($options)) {
			$options = array('htr_top_post_num' =>'5','htr_top_post_date' =>'7','htr_top_post_title' => '');
		}
		if ($_POST['widget_htr_top_post-submit']) {
			$options['htr_top_post_title'] = stripslashes($_POST['htr_top_post_title']);
			$options['htr_top_post_num'] = stripslashes($_POST['htr_top_post_num']);
			$options['htr_top_post_date'] = stripslashes($_POST['htr_top_post_date']);
			update_option('widget_htr_top_post', $options);
		}
		echo '<p style="text-align: left;"><label for="htr_top_post_title">Title: ';
		echo '</label><input type="text" name="htr_top_post_title" id="htr_top_post_title" value="'.stripslashes($options['htr_top_post_title']).'" /></p>'."\n";
		echo '<p style="text-align: left;"><label for="htr_top_post_num">Number posts: ';
		echo '</label><input type="text" name="htr_top_post_num" id="htr_top_post_num" value="'.stripslashes($options['htr_top_post_num']).'" size="3"/></p>'."\n";
		echo '<p style="text-align: left;"><label for="htr_top_post_date">Top Date: ';
		echo '</label><input type="text" name="htr_top_post_date" id="htr_top_post_date" value="'.stripslashes($options['htr_top_post_date']).'" size="3"/></p>'."\n";
		echo '<input type="hidden" id="widget_htr_top_post-submit" name="widget_htr_top_post-submit" value="1" />'."\n";
	}
	// Register Widgets
	register_sidebar_widget('HuTruc Top Posts', 'widget_htr_top_post');
	register_widget_control('HuTruc Top Posts', 'widget_htr_top_post_options');
}
widget_htr_top_post_init();
function htr_top_posts2($num = 10,$date=7) {
	$recent_posts = htr_top_posts($num,$date);
	foreach($recent_posts as $post){
	?>
	
		<li><a href="<?php echo get_permalink($post["ID"]); ?>" title="Look <?php echo $post["post_title"]; ?>" ><?php echo $post["post_title"]; ?></a> <span class="cmttitle"><?php echo $post["comment_count"]; ?></span></li><?php
	} 
}
/***** End #Top trong tuan http://hutruc.com *****/

Vậy là xong rồi, bạn có thể tham khảo các bài hướng dẫn về code trước đây của HT để thêm ảnh minh họa, nội dung bài viết,… cho thêm sinh động.

Demo trên Hư Trúc Blog

Demo trên Hư Trúc Blog

Chúc các bạn thành công!