Hướng dẫn hiển thị lượt xem bài viết cho wordpress

  • Thread starter Thread starter AdminAdmin is verified member.
  • Start date Start date
Admin

AdminAdmin is verified member.

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 :


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>
Xong
 

Facebook Comments

Similar threads

Admin
Replies
0
Views
2K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
962
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
2K
AdminAdmin is verified member.
Admin
cuongpro9x
Replies
0
Views
1K
cuongpro9xcuongpro9x is verified member.
cuongpro9x
thanhchiit
Replies
5
Views
2K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
2K
AdminAdmin is verified member.
Admin
Admin
Replies
3
Views
3K
Hoichemgio9x
H
cuongpro9x
Replies
3
Views
2K
cuongpro9xcuongpro9x is verified member.
cuongpro9x
Back
Top