cách code thêm lượt view vào blog
Bước 1: add code dưới vào function
function td_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, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } } remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); //show view count function td_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; } return $count . ' Lượt xem'; }
Bước 2: các bạn add code này vào vòng lặp blog | thường sẽ mục single.php
td_set_post_views(get_the_ID());
Bước 3: các bạn muốn hiển thị view đó ở mục nào trong bài viết chỉ cần add thêm code get nó ra là xong
td_get_post_views(get_the_ID());