Categories: wordpress

HOW TO SHOW NUMBER OF VIEW (VIEW COUNT) IN WORDPRESS? [for wordpress developer]

First of all you ned to set post view count using post meta.
Use the following code to set post

function setPostViews($postID) {
$countKey = 'post_views_count';
$count = get_post_meta($postID, $countKey, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $countKey);
add_post_meta($postID, $countKey, '0');
}else{
$count++;
update_post_meta($postID, $countKey, $count);
}
}

The second Process is to add function to show the total view in the post
you can use the follwing code in function.php

function pp_getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}

The next step is to display the count in the post. In the post loop (ie. in single.php) you have to add the following code to display the number of post view count.

<?php pp_getPostViews(get_the_ID()); ?>

Now the last and the most important step is to put the following code for increasing the count in single.php

<?php setPostViews(get_the_ID()); ?>
jaminrai

Recent Posts

Class XII | Assignment| Chapter 2: Data Communication and Networking

MCQ 1) … Refers to the sending, receiving and processing of information by electronic means.…

6 days ago

Class XI [Chapter 1: Computer System] Assignment

MCQ Which of the following is volatile storage? Backup b. RAM                 c. ROM                 d. Tape…

1 week ago

Class XI Chapter 2 Number system [ASSIGNMENT]

MCQ 1. We have ....... universal gatges a. 1      b. 0     c.…

2 weeks ago

What is Topology?

  DOWNLOAD  PDF  HERE!!!!!! topology

3 weeks ago

Class XII | Assignment 2 ( C Structure)

STRUCTURE (Submit after Mid Term , 15 Ashad 2083) A) MCQ 1. What is the…

1 month ago

Class XII Assignment 1 ( C Function )

FUNCTION (Assignment) Submit Before Mid Term MCQ 1. What is the output of this statement…

1 month ago