Home> CMS Tutorial> WordPress> body text

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

藏色散人
Release: 2021-03-19 17:15:55
forward
2520 people have browsed it

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);
Copy after login

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 

阅读:

Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!