I thought it would be a good idea to show how many visits a blog post had since it was published. One of the web tracking tools I use is WordPress.com Stats, and it's the easiest to get the page view count from. So after some googling I found the best way to do so is to use the WordPress.com Stats Helper plugin and two lines of PHP code to display the number.

Being the most popular web tracking tool for WordPress, WordPress.com Stats needs no introduction. And WordPress.com Stats Helper is a plugin developed by Vlad Bailescu that allows you to retrieve data from WordPress.com Stats. The plugin was not updated for a long time, since November 2008, but it still does the job.

I placed the code below in the single.php page of the current theme. Which is the page used to display a single post.

<?php 
if (function_exists('wpcomstats_visits')) { 
    wpcomstats_visits('', ' Visits', get_the_ID(), 0); 
} 
?>

The function_exists function checks the list of built-in and user-defined functions, and returns TRUE if it's found. I used it to make sure the function wpcomstats_visits, which is provided by the plugin, is available and no error is displayed to the user if the plugin is disabled or removed.

The wpcomstats_visits function shows the views count and takes the following 4 arguments: the text to display before the views count, the text to display after the views count, the id of the post or page and the number of days to return the stats of. To get the id of the current post or page you just pass the WordPress function get_the_ID in the third argument.

I hope you find this helpful.