Hiển thị Views WordPress Để đếm số lượt xem (views) của một bài viết và hiển thị số lượt xem đó trên danh sách các bài viết trong WordPress, bạn có thể làm theo các bước sau:
Bạn sẽ cần tạo một trường tùy chỉnh để lưu trữ số lượt xem. Trường này có thể được cập nhật mỗi khi bài viết được truy cập.
Thêm đoạn code sau vào file functions.php của theme bạn đang sử dụng:
// Hàm để đếm lượt xem bài viết function set_post_views($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if ($count == '') { $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '1'); } else { $count++; update_post_meta($postID, $count_key, $count); } } // Gọi hàm đếm lượt xem khi bài viết được truy cập function track_post_views ($post_id) { if (!is_single()) return; if (empty ($post_id)) { global $post; $post_id = $post->ID; } set_post_views($post_id); } add_action('wp_head', 'track_post_views'); // Ngăn chặn các bot làm tăng lượt xem remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); // Hàm để hiển thị số lượt xem function get_post_views($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0 View"; } return $count.' Views'; }
Để hiển thị số lượt xem trong danh sách bài viết (chẳng hạn trong index.php, archive.php, hoặc các template liên quan), bạn có thể sử dụng hàm get_post_views() mà chúng ta vừa tạo:
<?php // Trong vòng lặp hiển thị danh sách bài viết (loop) if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="post"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p><?php echo get_post_views(get_the_ID()); ?></p> </div> <?php endwhile; endif; ?>
Bạn có thể tùy chỉnh CSS để hiển thị số lượt xem theo ý muốn. Ví dụ:
.post p { font-size: 14px; color: #555; }
Với đoạn code này, bạn sẽ có thể đếm và hiển thị số lượt xem của bài viết trong danh sách bài viết.
Để hiển thị số lượng lượt xem của bài viết trong danh sách bài viết trên trang quản trị WordPress, bạn cần thêm cột mới vào bảng danh sách bài viết và hiển thị số lượt xem trong cột đó. Dưới đây là các bước để thực hiện điều này:
Thêm đoạn mã sau vào file functions.php của theme hoặc plugin của bạn để thêm cột mới vào trang quản trị danh sách bài viết.
// Thêm cột mới vào danh sách bài viết function add_views_column($columns) { $columns['post_views'] = 'Views'; return $columns; } add_filter('manage_posts_columns', 'add_views_column'); // Hiển thị số lượt xem trong cột mới function display_views_column($column_name, $post_id) { if ($column_name == 'post_views') { echo get_post_views($post_id); } } add_action('manage_posts_custom_column', 'display_views_column', 10, 2);
Nếu bạn muốn định dạng số lượt xem, chẳng hạn như thêm đơn vị “views” hay sử dụng định dạng số, bạn có thể sửa đổi hàm get_post_views():
function get_post_views($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count == ''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0 Views"; } return $count . ' Views'; }
Bạn có thể tùy chỉnh CSS để điều chỉnh hiển thị cột nếu cần. Ví dụ:
.wp-list-table .column-post_views { width: 100px; text-align: center; }
Với các bước này, bạn sẽ có một cột mới trong trang quản trị danh sách bài viết để hiển thị số lượt xem của từng bài viết.
Leave A Comment