Name Update Time
Netflix September 14, 2024 10:30 am
Disney+ September 10, 2024 10:09 am
Max September 14, 2024 10:20 am
ChatGPT 4 September 14, 2024 2:26 pm
Spotify September 14, 2024 2:08 pm
Prime Video September 14, 2024 2:22 pm
Codecademy September 14, 2024 2:13 pm
Grammarly September 14, 2024 4:45 pm
Canva Pro September 12, 2024 2:24 pm
Udemy Premium Cookies September 2, 2024 2:53 pm

When you build your own website using WordPress, if you want to display the page views of each article on the website, you will generally use a WordPress page views plug-in, which can conveniently count the page views of each article on our website.

First, add the following code to the wordpress template function functions.php file in the website backend:
/*Show article views*/
function getPostViews($postID){
$count = get_post_meta($postID,’views’, true);
if($count==”){
delete_post_meta($postID,’views’);
add_post_meta($postID,’views’, ‘0’);
return “0”;
}
return $count.”;
}
function setPostViews($postID) {
$count = get_post_meta($postID,’views’, true);
if($count==”){
$count = 0;
delete_post_meta($postID,’views’);
add_post_meta($postID,’views’, ‘0’);
}else{
$count++;
update_post_meta($postID,’views’, $count);
}
}
It can be used wherever you need to display the number of views, including the homepage, category page, and article page.
Add the call code for the number of views:
<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?>