how to add wordpress post views counter without Plugin

Do you want to WordPress post views counter without Plugin? Here are your right tutorials. Basically, You can add post views Counter With plugin but Someone Don’t like add any plugin. however, Today I will show You how to make Post views counter Just Three Steps.

Let’s Started.

At first, go to your theme editorย  ย and selected (function.php)

how to add wordpress post views counter without Plugin
how to add wordpress post views counter without Plugin

 

Then Just Paste below Code in the (function.php) save.

<?php
// function to display number of posts.
function 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 View";
}
return $count.' ';
}
// function to count views.
function setPostViews($postID) {
$count_key = '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);
}
}

?>

 

Then You need go to Single.phpย  and Paste below Code and Save.

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

 

Final Step Paste This below Code where You want to Show Post Counter.

<?php
echo getPostViews(get_the_ID());
?> views

 

 

Thanks for reading post.If you have any problem.Please drop your problem on comment box.

 

 

 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *