Implement real WordPress reading page statistics through code

888u

Last update at :2024-01-30,Edit by888u

We see that many themes that come with WordPress have visitor reading numbers displayed, but sometimes they will automatically increase when we refresh the page. In fact, this kind of user experience is not accurate enough. It should be that one user only displays the reading amount once, and it will not increase due to another refresh. So when we create a WordPress theme, we can use the following method to achieve a browsing volume that does not change due to refresh.

First, custom function

Add code to your theme’s Functions.php file

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 "0"; } return $count; } 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); } }

Second, add page code

Add the required display code to the Single.php page

Recommended site searches: registration-free jsp space, the best virtual host server in Hong Kong abroad, French proxy server, IP address query, vps to ip, mainland China registration-free cdn, mainland China free space, US host rental, query domain name Filing,

Implement real WordPress reading page statistics through code

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码