Mở file functions.php trong child theme của bạn (nếu chưa có child theme, bạn nên tạo child theme để tránh mất thay đổi khi update theme). Thêm đoạn code sau:
// Hàm lấy số lượng Like hoặc Dislike
function ip_get_like_count($type = 'likes') {
$current_count = get_post_meta(get_the_id(), $type, true);
return ($current_count ? $current_count : 0);
}
// Hàm xử lý Like và Dislike
function ip_process_like() {
if (is_singular('post')) {
if (isset($_GET['post_action']) && isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'ip_like_action')) {
$processed_like = false;
$redirect = false;
// Kiểm tra hành động Like hoặc Dislike
if ($_GET['post_action'] == 'like') {
$like_count = get_post_meta(get_the_id(), 'likes', true);
$like_count = $like_count ? $like_count + 1 : 1;
$processed_like = update_post_meta(get_the_id(), 'likes', $like_count);
} elseif ($_GET['post_action'] == 'dislike') {
$dislike_count = get_post_meta(get_the_id(), 'dislikes', true);
$dislike_count = $dislike_count ? $dislike_count + 1 : 1;
$processed_like = update_post_meta(get_the_id(), 'dislikes', $dislike_count);
}
if ($processed_like) {
$redirect = get_the_permalink(); // Trả về trang bài viết sau khi cập nhật
}
if ($redirect) {
wp_redirect($redirect); // Tải lại trang để hiển thị kết quả mới
die;
}
}
}
}
add_action('template_redirect', 'ip_process_like');
CSS tí cho đẹp
/* CSS nút hữu ích */
.ok-like a {
margin-right: 10px;
text-decoration: none;
color: #111;
font-size: 13px;
font-weight: bold;
background: #fff444;
padding: 5px 8px 5px 8px;
border-radius: 7px;
display: inline-block;
}
.ok-like a:hover {
color: #fff;
background: #0c0;
}
.title-like {
color: #ccc;
font-size: 15px;
}
.yes-likes {
padding: 20px;
background: #4b238a;
text-align: center;
border-radius: 10px;
}
@media (max-width: 400px) {
.ok-like a {
font-size: 10px;
}
}
/*---------------------------------------------*/
Chào ! Bạn thấy nội dung này thế nào?





