I have seen many premium WordPress themes
showing Post views count right below Posts and i thought that its only
for premium themes but when i find about this topic on internet i found
that its very easy to show post views count on any WordPress blog. Again
thanks to the WordPress plugins which make our lives easier. But for
those who don’t like to use too many plugin’s on there blog there is
also a direct code which you have to put into your theme files. Showing
post views will be a nice thing to do as it help readers to select
popular post based on views.
WordPress PostViews Plugin.
The easiest way to show post views count is obviously via installing plugin. The plugin we use is WP-PostViews which you can download from official WordPress website by clicking here. Download the plugin, install it and than activate it. Now go to
Now comes the second part. Open your theme’s single.php file or any file where you want to display the post views count and search for the heading code.
Show Post Views Count on Posts without Plugin.
Now for those who hate plugins like me there is a piece of code for you through which you can easily show post views count on any page. First of all open your blog theme’s functions.php file and paste the following code in it.
source
WordPress PostViews Plugin.
The easiest way to show post views count is obviously via installing plugin. The plugin we use is WP-PostViews which you can download from official WordPress website by clicking here. Download the plugin, install it and than activate it. Now go to
WP-Admin -> Settings -> PostViews
and configure the plugin options.Now comes the second part. Open your theme’s single.php file or any file where you want to display the post views count and search for the heading code.
<?php the_title(); ?>Now right below this code add following code and update the file.
<?php if(function_exists(‘the_views’)) { the_views(); } ?>Refresh your blog and you will see post views count on posts starting from zero.
Show Post Views Count on Posts without Plugin.
Now for those who hate plugins like me there is a piece of code for you through which you can easily show post views count on any page. First of all open your blog theme’s functions.php file and paste the following code in it.
Now open single.php file and paste the following code right after while loop.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); } }
To display the post view count simply paste the code where you want to actually display the post views count.<?php setPostViews(get_the_ID()); ?>
That’s it very easy and simple way to display post views count in any page in WordPress blog.<?php echo getPostViews(get_the_ID()); ?>
source