Sometime we want to check the total views of the particular post… so here is the solution by using of this code you can check how many times your post is viewed.
- Add this to theme Function.php file
function wpb_set_post_views($postID) {
$count_key = 'wpb_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
} else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
2. This one also into Function.php file
function wpb_get_post_views($postID){
$count_key = 'wpb_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 View";
}
return $count.' Views';
}
3. Then put this code to Single.php
<?php wpb_set_post_views(get_the_ID()); ?>
4. And then put this code where you want to show post count..
<?php echo wpb_get_post_views(get_the_ID()); ?>
5. Thats it 🙂
You can download the full code of Plugin here… after activating this plugin you need to follow just last 2 steps and also you will see the instruction area at the back-end like this..
Download