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:
- A wordpress self hosted blog
- A little understanding of how to paste the code 😀
- 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
Wamiq you should also use ajax for this. because when you use cache plugin then it do not work fine. I have used that ajax trick.if need some help then tell me 😉
Thank you for the response but instead of using cache,try to use fragmented cache in w3 for things like voting, view counts etc.
how to solve .use ajax. i cached by cos html cache.thank u
I really didn’t understand, what you are talking about, will you be please more specific and clear? And you can always contact me through contact forum.
Not working with my theme i added the code but it is not showing up. help me please
Which theme are you using? Plus are you sure that you used these two codes in single.php?
setPostViews(get_the_ID()); to set the view number and then echo getPostViews(get_the_ID());; to get the post views!
Hi alli, Open “single.php” and find out bellow codes
Now paste the bellow code under finded codes.
Open index.php or post-loop.php file and paste bellow code; you want to show post views counter.
Hello,
Than you for this tutorial, but I have a little one problem. My counter shows a one view twice. After refresh a page I have eg. 3 views instead of 1.
Thank you for you response!
You must have used it somewhere in single.php where this code get’s repeated 3 times setPostViews(get_the_ID()); So please analyse your code as it’s repetition will keep on adding extra views.
Thanks for response. I try to find it, but I dont see it there more times. If you can help me I will be grateful. Here is my single.php http://pastebin.com/9nqX2eyv
OK! I have spotted two wp_query, so the first solution is to, reset all your queries just at the point where those looping queries end up,with this tag
wp_reset_query();
![…] Visit counter for wordpress posts without a plugin […]