Menu
tomorrowland.com
  • Contact
  • Podcast
  • Guides
    • Mac Pro Optical Drive Install – Part 1
    • Mac Pro Optical Drive Install – Part 2
    • Mac Pro Optical Drive Install – Part 3
tomorrowland.com

WordPress – Simple Post Views Counter Plugin

Posted on November 12, 2008November 18, 2010 by Michael

WP-PostViews is a free plugin by Lester Chan that counts views for each post. Installing and activating the plugin begins the tracking, but it’s up to you to add code to your theme somewhere to display the view count. No offense to Lester, but the documentation left a little to be figured out. What I mean by this is that it doesn’t explain how it works or what it counts as a view. With a little testing I figured out that it only counts the view when someone loads the full post. Even if the entire text of the post is seen on the main page – meaning that you didn’t use the “more” tag – it won’t increment the view count unless the post is loaded by itself. This gives misleading results because if the entire post is on your main page it’s unlikely that anyone will click through. Therefore even a popular post could show only a few views. This is something that I might try to fix eventually.

Of course counters like this one are only good to give you an idea of traffic. They can’t be considered as technically accurate because of bots, self-inflicted views, and reloads. Some would argue that it’s just for the novelty of watching the number get bigger and feeling good that people are reading your blog. However, I’m using it as a metric to see overall traffic trends and see what types of posts are more popular.

Based on the documentation, the code to show the view count on each post has to be manually added to your theme. In your wordpress dashboard go to the Theme Editor under the Design tab:

The highlighted code above is what I added. It checks to see if a function called ‘the_views’ exists and if so it is launched. That function displays your view count based on the options you’ve set up. Where the count shows within your post block is determined by where you place this code. It is up to you to figure out the proper placement and will be different depending on your theme. It just has to be somewhere inside the main loop where posts are displayed. Through trial and error I’ve settled on placing it right below the tags and right above the comments link. I also put <div>’s around it so that it will be on it’s own line.

Of course this only places the count on the posts as they display on your main index (your home page). You also have to add this code to the script that is used for single posts and pages. For my theme they were called single.php, and page.php.

Now to the mod. The initial thing I didn’t like about the plugin is that if a post had zero views it showed “Views: 0”. It should be impossible to have zero views because you have to view it to see the 0. Even the first viewer should see “Views: 1” as they are the FIRST viewer. Clearly it is incrementing the counter after showing the counter. Maybe Lester or someone else can chime in the comments and tell me if I’m doing something wrong. But from from what I can tell, since it only increments the counter when the full post is loaded, the value will be zero when seen on the main page until someone clicks through to the see the full post.

I don’t like seeing Views: 0 and would rather not see anything at all until it has a value. So the mod I did was to only show the “Views:….” if the count is at least 1. You could have it not show the number until it’s over 10 (or some other threshold). Not being an expert PHP programmer I just guessed at how to change the code.

Editing a plugin is done via the Plugin Edior found under the Plugins Tab:

Editing plugins can be dangerous. If you cause a program error the plugin will disable itself – which could have some repercussions that I don’t know about. The highlighted text in the above image shows the code I changed. Originally the highlighted code was only:

echo $output;

By adding the if($post_views > 0) { …. } around the echo I disabled the echo when the number of views is zero. If you wanted to only show the count when it reaches a higher number, just replace that zero with your threshold.

 

9 thoughts on “WordPress – Simple Post Views Counter Plugin”

  1. GaMerZ says:
    November 12, 2008 at 12:02 pm

    Yeap, it counts only when the post is clicked on as in the post permalink and it will be counted as one. It is a very simple plugin no data is logged and hence if you refresh it will +1 again.

  2. admin says:
    November 12, 2008 at 5:00 pm

    Cool! Glad you commented. Yeah, simple is good. I can see why you wouldn’t want it to advance the counter unless they actually go to the post page, but I can also see why you would want it to advance if the full post is on your home page.

    Is it a simple tweak to change that? What calls the counter so that it could be added to the main loop?

  3. admin says:
    November 13, 2008 at 3:43 am

    GaMerZ, in writing my post of RED tonight I discovered that the counter resets to zero every time I edit the page.

  4. GaMerZ says:
    November 13, 2008 at 11:49 am

    The counter is hooked onto “wp_head”, function name is “process_postviews”

    I have not seen this 0 problem before as there is no way it gets rest to 0, unless you mean the “views” meta key gets deleted.

  5. admin says:
    November 13, 2008 at 4:19 pm

    hmm.. it was doing it last night but now it’s not. I don’t want to experiment with the Red post, but will the next time I post something new. I also now see that views is a custom field that I can change anyway.

    I’ll play around with it. Thanks again for being available to discuss.

  6. Michael Smith says:
    November 18, 2008 at 2:01 am

    Ok I figured out what was going on there. I was editing the initial post and checking it in a 2nd Safari window. Each time I loaded the post in the 2nd window it would advance the postview counter. However, each time I edited and saved the post in the 1st window it reset the postview counter to 1. This is because the postview counter is an additional field which is re-set when I saved window #1. Confused yet?

    Basically the plug in works and the viewcount resetting to 1 was USER ERROR.

  7. Indinnyloyado says:
    December 27, 2008 at 8:52 am

    tcqugoduspzdfdoawell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch 😉

  8. Musa says:
    March 26, 2009 at 3:36 pm

    I want this plugin but it seems so complicated to get it running. I'm not so technical, can anybody help

  9. Michael Smith says:
    March 29, 2009 at 11:03 pm

    install and activate the plugin.. then add the highlighted code to your index.php, to single.php and to page.php.

    The plug in does the work.. it has a function in it called the_views. This function is called inside your code to advance the counter and display the results.

    the MOD i did to make it not display if it's zero is something that you don't have to do (probably shouldn't do unless you're a more advanced user).

Comments are closed.

Recent Comments

Post Categories

  • Audio (2)
  • Deals (4)
  • Distractions (23)
  • FAIL (2)
  • Guide (1)
  • Home Video (27)
  • Humor (1)
  • iPad (2)
  • iPhone (9)
  • iPod (4)
  • Life Tech (10)
  • Pet Tech (1)
  • Pro Video (7)
  • Software (1)
  • System Tech (36)
  • tablet (3)
  • TLP (2)
  • What's Old (6)
  • What's Up (19)
  • WordPress (4)

Resources

  • Creative Cow free After Effects Tutorials
  • Engadget
  • Gizmodo – the gadget guide
  • iPhone POV
  • Jayse Hansen’s training site
  • The Unofficial Apple Weblog (TUAW)
  • Video Copilot – Tutorials Hosted by Andrew Kramer

RSS Subscribe to RSS Feed

©2023 tomorrowland.com | Powered by WordPress & Superb Themes