Code function trong childthem flatsome
//dem view
function update_post_view_log($post_id) {
if (!is_numeric($post_id)) return;
$views = get_post_meta($post_id, '_post_views', true);
$views = ($views == '') ? 0 : intval($views);
$views++;
update_post_meta($post_id, '_post_views', $views);
// Xóa cache của LiteSpeed nếu cần
if (function_exists('litespeed_purge_post')) {
litespeed_purge_post($post_id);
litespeed_purge_url(home_url('/'));
}
}
function get_post_view_from_log($post_id) {
$views = get_post_meta($post_id, '_post_views', true);
return ($views) ? intval($views) : 0;
}
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/post-views/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => function ($request) {
$post_id = intval($request['id']);
return new WP_REST_Response(get_post_view_from_log($post_id), 200);
},
));
});
add_action('wp_ajax_update_post_view', 'ajax_update_post_view');
add_action('wp_ajax_nopriv_update_post_view', 'ajax_update_post_view');
function ajax_update_post_view() {
if (isset($_POST['post_id']) && is_numeric($_POST['post_id'])) {
update_post_view_log($_POST['post_id']);
echo get_post_view_from_log($_POST['post_id']);
}
wp_die();
}
add_action('wp_footer', function () {
if (is_single()) {
?>
<script>
document.addEventListener("DOMContentLoaded", function () {
let postID = <?php echo get_the_ID(); ?>;
fetch("<?php echo admin_url('admin-ajax.php'); ?>", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: "action=update_post_view&post_id=" + postID
}).then(response => response.text()).then(data => {
document.querySelector(".view-count").textContent = data;
});
});
</script>
<?php
}
});
add_action('wp_footer', function () {
?>
<script>
function updatePostViews() {
let postIDs = [];
let elements = document.querySelectorAll(".view-count");
elements.forEach(el => {
let postID = el.getAttribute("data-post-id");
if (postID) postIDs.push(postID);
});
if (postIDs.length > 0) {
fetch("<?php echo get_rest_url(null, 'custom/v1/post-views-bulk'); ?>?ids=" + postIDs.join(","))
.then(response => response.json())
.then(data => {
elements.forEach(el => {
let postID = el.getAttribute("data-post-id");
if (data[postID] !== undefined) {
el.textContent = data[postID];
}
});
});
}
}
document.addEventListener("DOMContentLoaded", updatePostViews);
window.addEventListener("pageshow", function(event) {
if (event.persisted) { // Kiểm tra nếu trang được load từ bfcache
updatePostViews();
}
});
</script>
<?php
});
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/post-views-bulk', array(
'methods' => 'GET',
'callback' => function ($request) {
$ids = explode(',', $request->get_param('ids'));
$results = [];
foreach ($ids as $id) {
$post_id = intval($id);
$results[$post_id] = get_post_meta($post_id, '_post_views', true) ?: 0;
}
return new WP_REST_Response($results, 200);
},
));
});
//dem view
Hiển thị trong bài viết
/themes/flatsome/template-parts/posts/partials/entry-title.php
if (get_theme_mod('blog_single_header_title', 1)) :
echo '<h1 class="entry-title">' . get_the_title() . '</h1>';
$post_id = get_the_ID();
$post_views = get_post_meta($post_id, '_post_views', true);
$post_views = ($post_views) ? intval($post_views) : 0;
?>
<div class="post-meta-container">
<span class="post-meta"><i class="far fa-clock"></i> <?php echo get_the_date(); ?></span>
<span class="post-meta post-meta-views">
<i class="fa-solid fa-eye"></i>
<span class="view-count"><?php echo esc_html($post_views); ?></span> lượt xem
</span>
</div>
Hiện thị ngoài trang chủ
themes/flatsome/inc/shortcodes/blog_posts.php
<?php
if ((!has_post_thumbnail() && $show_date !== 'false') || $show_date == 'text') {
$post_id = get_the_ID();
?>
<div class="post-meta-container">
<span class="post-meta"><i class="far fa-clock"></i> <?php echo get_the_date(); ?></span>
<span class="post-meta post-meta-views">
<i class="fa-solid fa-eye"></i>
<span class="view-count" data-post-id="<?php echo esc_attr($post_id); ?>">0</span> lượt xem
</span>
</div>
<?php } ?>