Hướng dẫn thêm nút hữu ích vào bài viết
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
// 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'); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* 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; } } /*---------------------------------------------*/ |
Quý khách cần tư vấn thiết kế web giá rẻ, vui lòng nhấn nút bên dưới:
Chào ! Bạn thấy nội dung này thế nào?
Xem thêm:
- Hướng dẫn thêm nút Mua ngay sau nút Thêm vào giỏ hàng
- 28 tiêu chí SEO Onpage quan trọng nhất được Google công nhận hoặc khuyến nghị
- Tại sao tắt REST API thì Contact Form 7 (CF7) không hoạt động?
- SEO Từ Khóa: Làm Thế Nào Để Nhét Từ Khóa Vào Bài Viết Một Cách Tự Nhiên?
- Hướng dẫn code function Kiểm tra một số thông tin từ domain (tên miền) website