Wordpress

Visit Counter for WordPress Posts – Show Views Without Plugin

If you are a blogger and write good stuff on your blog then you will surely like to count for every visit. I will help you do it without any plugin since often plugins don’t give you the freedom of editing the things and this visit counter will count for every visit regardless of the IP address. Whoever will hit the post this visit counter for WordPress is going to count one view for it.

Benefit of Visit Counter for WordPress:

It will encourage your readers to look into the stuff which got so much views by the public. It will make them think that it’s surely a stuff which I need to view because curiosity is in human blood from the beginning. You can show the visit counter in the text form at any part of your blog.

Coding Part of the Visit Counter:

The coding part is real easy and you don’t need to cook anything. Everything has been pre-cooked. You just need to grab the code and put that into specific files of your theme structure. What you need is below:

  1. A wordpress self hosted blog
  2. A little understanding of how to paste the code 😀
  3. A little brain, Here you go 😛

Edit Functions.php of your WordPress Theme (Template):

Grab a good editor if you edit files VIA ftp and it will easily highlight your syntax for your ease. In my case I use Notepad++. However if you don’t use ftp then chances are that you will go to your WordPress Dashboard. And then Appearance>Editor>functions.php

However what every way you may acquire you just need to edit the file. Grab the following code and paste that into the end of functions.php without leaving any space at the end of file or else your blog will give you error.

/* Visit Counter For WordPress by Talkofweb.com */
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.' 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);
}
}

The first function getPostViews(); is going to get your posts visits and the other function setPostViews(); is going to set the posts views i-e is going to count the posts views for your WordPress posts.

Using The Above Functions in Single.php

It’s time to use these functions in the single.php to let your blog site count for every visit made to your post area. Let me tell you that single.php is the template file of your posts and you need to add this below given code code in the loop of your single.php file. You can again edit single.php of your WordPress theme in many ways. It just depends upon you. Grab the following code and paste this after the_content();. The below code will count for every visit using the setPostViews(); function.

setPostViews(get_the_ID());

Now! to show the counts done by the above code for every post of your WordPress blog. You need to put this code anywhere at your blog in the loop of your posts only. You can add this in the single.php to show posts views counts at posts only and similarly you can add this code to the loop of the homepage posts. Just keep it trying and you will find the exact place to show it. Copy and paste the below Code in your theme posts loop:

echo getPostViews(get_the_ID());;

You are done. Ask any queries in the comments below.

Update: Now Show Popular Posts using this Posts View Counter For WordPress!

Read Tutorial : Popular Posts in WordPress using the Visits counter for WordPress

12 Comments