Home>Article>CMS Tutorial> How to achieve number of article views in wordpress without plug-ins

How to achieve number of article views in wordpress without plug-ins

藏色散人
藏色散人 forward
2021-03-19 17:15:45 2529browse

The following tutorial column ofWordPresswill introduce to you how to achieve the number of article views in WordPress without plug-ins. I hope it will be helpful to friends in need!

How to achieve number of article views in wordpress without plug-ins

1. Thoughts

I thought at first that I should Is there any plug-in that can achieve this? After searching, the first one isPost Views Counter.

Before installing this plug-in, I thought about it, can I implement it myself? After all, if you put your hands into it, you will gain something.

Before searching, what I thought of was to add a field to thewp_posttable, and then save the data when the article is opened, so that the reading count of the article can be saved persistently.
But WordPress is written in PHP and MySQL adds fields, which is quite time-consuming for me on the front end. For example, how to operate the database using PHP, and how to add fields using PHP? It is estimated that it will take at least half a day or even a day to complete.

Is there an easier way?
Because I have messed with the wordpress database before, I know what tables there are. So it suddenly occurred to me that there is awp_postmetatable. From the literal point of view, it should be possible to add a field or start from this table.
How to achieve number of article views in wordpress without plug-ins

meta_id is the id, post_id is the article id, meta_key and meta_value are the key-value pair information of the article.

2. Methods provided by wordpress

How to operate this table?
wordpress provides several methods:

add_post_meta($post_id, $meta_key, $meta_value, $unique); get_post_meta($post_id, $meta_key, $single); update_post_meta($post_id, $meta_key, $meta_value, $prev_value); delete_post_meta($post_id, $meta_key, $meta_value);

3. Specific code implementation

How to use it?
First add the function encapsulation of add and get in thefunction.phpfile, and then call it in thetemplate-parts/content-single.phpfile.

// function.php function addPostViews($postId) { $key = 'post_views'; $value = get_post_meta($postId, $key, true); if($value == ''){ $value = 0; delete_post_meta($postId, $key); add_post_meta($postId, $key, $value); }else{ $value++; update_post_meta($postId, $key, $value); } } function getPostViews($postId){ $key = 'post_views'; $value = get_post_meta($postId, $key, true); if($value == ''){ $value = 0; delete_post_meta($postId, $key); add_post_meta($postId, $key, $value); return $value; } return $value; } // template-parts/content-single.php 

阅读:

The above is the detailed content of How to achieve number of article views in wordpress without plug-ins. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete