Home > Article > CMS Tutorial > How to display article browsing statistics without WordPress plug-in
How to display article browsing statistics without WordPress plug-in?
Wordpress has no plug-in to display the number of article views, with an explanation of the principle. There are many articles about this on the Internet, but if you don’t understand the principle, you will inevitably feel a little uneasy, fearing that there are loopholes in the code that will affect the website. normal access. Let’s explain the principle below. The code has been tested and passed on wordpress version 3.4.1
Recommended: wordpress tutorial
Add article Number of views
Wordpress itself does not have a statistical function for the number of article views, so you must first record the number of article views before you can read the number of views when displaying the article. The article table in the wordpress database is wp_posts, which does not have a field for the number of views. If you add the field yourself, the changes will be huge. A better way is to record the data in the wp_postmeta table. This table is used to record some article extension data. The implementation code is as follows:
The code is as follows:
/** * 设置文章的浏览次数 * * @param int $postID 文章编号 */ function setPostViews($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if ($count == '') { add_post_meta($postID, $count_key, '1'); } else { $count++; update_post_meta($postID, $count_key, $count); } }
The process of the code is to first obtain the number of article views from wp_postmeta, and then make a judgment: if the data of the number of article views is not obtained, the initial The value is set to 1; otherwise, the number of views is increased by 1 and the number of article views is updated.
Finally, just copy this code to the theme’s functions.php file and call it in the main loop of the article page (single.php). The calling code is as follows:
The code is as follows:
<?php setPostViews(get_the_ID()); ?>
Get the number of article views
You can read the number of article views after recording them. The code is as follows:
The code is as follows:
function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ return 1; } return $count; }
The code process is to get the number of views of the article from wp_postmeta. If not, return 1, otherwise return the number of views.
Copy this code to the theme’s functions.php file and call this method where needed.
Summary
The popular version on the Internet calls delete_post_meta. After careful inspection of the code, it is found that it is not necessary. There is also a postviews plug-in that can also achieve similar functions, but according to the official website, it is only compatible with WordPress version 3.3.2.
The above is the detailed content of How to display article browsing statistics without WordPress plug-in. For more information, please follow other related articles on the PHP Chinese website!