Home > CMS Tutorial > WordPress > How to display article browsing statistics without WordPress plug-in

How to display article browsing statistics without WordPress plug-in

藏色散人
Release: 2020-01-02 09:36:17
Original
1966 people have browsed it

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

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

How to display article browsing statistics without WordPress plug-in

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 = &#39;post_views_count&#39;;
$count = get_post_meta($postID, $count_key, true);
if($count==&#39;&#39;){
return 1;
}
return $count;
}
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template