
Admin
Well-Known Member
Staff member
Administrator
Đầu tiên bạn mở file functions.php trong folder giao diện của bạn và nhập đoạn code sau :
Kế tiếp các bạn mở file single.php hoặc post.php cũng nằm trong folder giao diện của bạn và nhập đoạn code sau vào sau tên bài viết (ai muốn ở trang chủ thì thêm vào index cho hợp lý nhá
Ở đây mình sẽ có đoạn code như sau:
Xong
PHP:
functiongetPostViews($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 Lượt Xem";
}
return $count.' Lượt Xem';
}
function setPostViews($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);
}
}
Kế tiếp các bạn mở file single.php hoặc post.php cũng nằm trong folder giao diện của bạn và nhập đoạn code sau vào sau tên bài viết (ai muốn ở trang chủ thì thêm vào index cho hợp lý nhá
PHP:
<?php setPostViews(get_the_ID()); ?>
<?php echo getPostViews(get_the_ID()); ?>
Ở đây mình sẽ có đoạn code như sau:
PHP:
Bài viết: <b><?php the_title(); ?></b><br/>Có <b><?php setPostViews(get_the_ID()); ?><?php echo getPostViews(get_the_ID()); ?></b>