Bước 1: Chèn code đếm lượt xem vào cuối file function.php của theme Flatsome. Lưu ý chỉ nên sử dụng child theme để sau này nâng cấp đỡ gặp vấn đề nhé
// hàm này dùng để set và update số lượt người xem bài viết
$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);
}
}
//02 code hiển thị số lượt xem trong dashboard Blog
add_action( ‘wp_enqueue_scripts’, ‘load_dashicons_front_end’ );
function load_dashicons_front_end() {
wp_enqueue_style( ‘dashicons’ );
}
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());
}
}
//03 code hiển thị số lượt xem trong dashboard woocommercer
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 );
Đối với bộ đếm trong woocommerce
Tìm file: flatsome\woocommerce\content-single-product.php thêm vào dòng 22 đoạn sau:
thêm dòng này vào dòng 22
/setPostViews(get_the_ID());
Sau khi thao tác xong bạn lưu lại và nhớ thêm css nhé
.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;
}
ok xong rồi nhé , Chúc bạn thành công