Thêm code đếm và hiển thị số lượt xem bài viết
Bước 1: Chèn code đếm lượt xem vào cuối file function.php của theme đang sử dụng:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPostViews($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 "01 lu?t xem"; | |
} | |
return $count.' lu?t xem'; | |
} | |
// code dem luot 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); | |
} | |
} | |
// code hien thi luot xem trong daskboard | |
add_filter('manage_posts_columns', 'posts_column_views'); | |
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2); | |
function posts_column_views($defaults){ | |
$defaults['post_views'] = __('Views'); | |
return $defaults; | |
} | |
function posts_custom_column_views($column_name, $id){ | |
if($column_name === 'post_views'){ | |
echo getPostViews(get_the_ID()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php setPostViews(get_the_ID()); ?> |
Bước 3: Thêm code sau vào file function.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function action_woocommerce_single_product_summary( ) { | |
echo '<span class="luot-xem">'; | |
echo getPostViews(get_the_ID()); | |
echo '</span>'; | |
} | |
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 5, 0 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.luot-xem:before { | |
content: "\f06e"; | |
color: #f96e5b; | |
display: inline-block; | |
font-size: 14px; | |
font-family: 'FontAwesome'; | |
margin: auto; | |
padding: 5px; | |
} | |
span.luot-xem { | |
color: #f96e5b; | |
} |
Lưu ý là số lượt xem bài viết nó sẽ được đếm kể từ lúc các bạn thực hiện theo hướng dẫn này. Trước đó nếu có triệu view nó cũng không có đếm đâu.
Đồng thời trong trang quản lý bài viết các bạn cũng có thêm trường thông tin về số lượt xem bài viết nữa. Trong đoạn code đầu tiên mình có ghi chú là code để hiển thị số lượt xem trong Dashboard, bạn nào không cần thì xóa đoạn đó đi là được.
Chúc các bạn thành công!
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.