Display Post Views in Any WordPress Themes

Recently I’ve been visiting some websites and noticed that some of them show how many times a post was visited; so I’ve decided to look into it. What I found was that it’s very easy to display post views in WordPress, you can use JetPack, which has a module to track visitors, or you can use a different plugin called WPPost Views. I’ll show you how to do it with both plugins.

Display Post Views in Any WordPress Themes

As I mentioned above, you can display it multiple ways, in this tutorial I show you two methods, with JetPack and with WP-Post Views plugins.

How to Display post Views in WordPress with JetPack

The JetPack plugin by Automattic has a module called WordPress.com Stats. After enabling it, you have to log in to your WordPress.com account and link your site to WordPress.com; This will allow you to track where your visitors are coming from; what keyword they used; where they go from your site and much more.

Or if you don’t want to use the whole JetPack plugin, you can install only the stats module by using this plugin JetPack Post View

To display the views for each post on the front-end, you need to open up your theme’s functions.php file and add the following code into it:

<?php

function render_stats($post_id) {
	$args = array(
		'days'=>-1,
		'limit'=>-1,
		'post_id'=>$post_id
	);

	$result = stats_get_csv('postviews', $args);

	$views = $result[0]['views'];
	return number_format_i18n($views);
}

And then you have to open up your theme’s single.php file and add the following code where you want to display the views:

<?php echo render_stats($post->ID); ?>

How to Display Post Views in WordPress with WP Post Views

To display Post Views with the WP-Post Views plugin, you need to download and install it first; And after you’ve installed it, go to the Admin Dashboard Settings->PostViews and there you can configure the plugin. Which views are counted, how the views are displayed and more.

To display the views on each post, open your single.php file and add the following code where-ever you want to display the views:

<?php if  (  function_exists('the_views')  )  {  the_views();  } ?>

Also if you use this plugin to display post views in wordpress, there’s a widget that comes along with the plugin that you can use to display your top viewed post and pages with.

I hope this tutorial helped you to display post views in wordpress; if you have an question or feedback related to this, feel free to leave a comment down below!

Get more stuff like this

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Add Comment

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.