Chủ đề Pix 12: Thêm chức năng bảng xếp hạng bình luận
Dưới đây là các bước chi tiết:
1. Thêm đoạn code sau vào file wp-content/themes/pix/functions.php:
// Đăng ký và tải CSS cho tường người đọc
function enqueue_readers_wall_styles() {
wp_enqueue_style('readers-wall-style', THEME_URL . '/css/readers-wall.css', array(), '1.0.0');
}
add_action('wp_enqueue_scripts', 'enqueue_readers_wall_styles');
// Hàm phụ trợ: Tạo danh sách xếp hạng
function generate_readers_list($title, $query, $limit) {
global $wpdb;
$output = '';
// Sử dụng transient để lưu trữ kết quả truy vấn
$transient_key = 'readers_wall_' . md5($query);
$wall = get_transient($transient_key);
if (false === $wall) {
$wall = $wpdb->get_results($query);
set_transient($transient_key, $wall, 3600); // Thời gian lưu cache là 1 giờ (3600 giây)
}
$output .= '<div class="readers-section">';
$output .= '<h2 class="entry-title">' . esc_html($title) . ' TOP' . esc_html($limit) . '</h2>';
if ($wall) {
$output .= "<ul class='readers-list'>";
foreach ($wall as $comment) {
$avatar = get_avatar($comment->comment_author_email, 64, get_bloginfo('wpurl') . '/avatar/default.jpg', '', array('loading' => 'lazy'));
$url = esc_url($comment->comment_author_url ? $comment->comment_author_url : "#");
$author = esc_html($comment->comment_author);
$count = intval($comment->cnt);
$tooltip = "{$author}<br>Comment: {$count}";
$output .= "<li>
<a rel='friend' target='_blank' href='{$url}' aria-describedby='tooltip-{$comment->comment_author_email}'>
{$avatar}
<div class='tooltip' id='tooltip-{$comment->comment_author_email}' role='tooltip'>{$tooltip}</div>
</a>
</li>";
}
$output .= "</ul>";
} else {
$output .= "<p>Không tìm thấy dữ liệu " . esc_html($title) . ".</p>";
}
$output .= '</div>';
return $output;
}
// Hàm shortcode: Tường người đọc
function readers_wall_shortcode() {
global $wpdb;
$output = '';
// Xếp hạng bình luận hàng năm
$query1 = $wpdb->prepare(
"SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email
FROM (
SELECT
LEFT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
WHERE comment_date BETWEEN DATE_SUB(NOW(), INTERVAL 1 YEAR) AND NOW()
AND post_password = ''
AND comment_approved = '1'
AND comment_author != %s
) AS tempcmt
GROUP BY comment_author_email
ORDER BY cnt DESC
LIMIT %d",
'Đoạn Tiên Sinh',
5
);
$output .= generate_readers_list('Bình luận hàng năm', $query1, 5);
// Xếp hạng bình luận tháng này
$query2 = $wpdb->prepare(
"SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email
FROM (
SELECT
LEFT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
WHERE DATE_FORMAT(comment_date, '%%Y-%%m') = DATE_FORMAT(NOW(), '%%Y-%%m')
AND post_password = ''
AND comment_approved = '1'
AND comment_author != %s
) AS tempcmt
GROUP BY comment_author_email
ORDER BY cnt DESC
LIMIT %d",
'Đoạn Tiên Sinh',
5
);
$output .= generate_readers_list('Bình luận tháng này', $query2, 5);
// Xếp hạng bình luận tuần này
$query3 = $wpdb->prepare(
"SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email
FROM (
SELECT
LEFT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
WHERE YEARWEEK(DATE_FORMAT(comment_date, '%%Y-%%m-%%d')) = YEARWEEK(NOW())
AND post_password = ''
AND comment_approved = '1'
AND comment_author != %s
) AS tempcmt
GROUP BY comment_author_email
ORDER BY cnt DESC
LIMIT %d",
'Đoạn Tiên Sinh',
5
);
$output .= generate_readers_list('Bình luận tuần này', $query3, 5);
return $output;
}
add_shortcode('readers_wall', 'readers_wall_shortcode');
2. Trong thư mục wp-content/themes/pix/, tạo folder css và thêm file readers-wall.css với nội dung sau: