How to show number of views in a WordPress Post

Seeing the number of views on an article is sometimes important to judge an article or to see the popularity. Showing the number of views of an article can be done or achieved in different ways. You can do through database or the easiest way is to use plugins. You can easily setup and show the number of views of a post to the users.

So, lets do it! Download this WordPress plugin and and install it. If you do not know how to install a plugin, you can read how to install a WordPress plugin. After installing the plugin, you need to place the below code into the post loop.

 

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

 

You can place the above code in main single loop, search page, archive page, category page and any custom page.

However, you can set some basic things of this little plugin.

You do not need to do anything with this setting but if you want to count views from guests, from everyone or just from the Registered users, you can select the option at the top.

However, you can do more with this plugin. Here are the things you can achieve with this plugin:

  1. Least Viewed Posts
  2. Most Viewed Posts
  3. Least Viewed posts by tag
  4. Most Viewed posts by tag
  5. Least Viewed posts from category
  6. Most Viewed posts from category

Now, lets see how we can implement these into our theme.

 

<?php if (function_exists('get_least_viewed')): ?>
<ul>
<?php get_least_viewed(); ?>
</ul>
<?php endif; ?>

 

The above code will fetch the least viewed posts.

 

<?php if (function_exists('get_most_viewed')): ?>
<ul>
<?php get_most_viewed(); ?>
</ul>
<?php endif; ?>

 

The above snippet will show the most viewed posts.

 

<?php if (function_exists('get_least_viewed_category')): ?>
<ul>
<?php get_least_viewed_category(); ?>
</ul>
<?php endif; ?>

 

the above code will show the least viewed category.

 

<?php if (function_exists('get_most_viewed_category')): ?>
<ul&>
<?php get_most_viewed_category(); ?>
</ul>
<?php endif; ?>

 

The above codes will show the most viewed category

 

<?php if (function_exists('get_least_viewed_tag')): ?>
<ul>
<?php get_least_viewed_tag(); ?>
</ul>
<?php endif; ?>

 

The above code will show the least viewed posts by tag.

 

<?php if (function_exists('get_most_viewed_tag')): ?>
<ul>
<?php get_most_viewed_tag(); ?>
</ul>
<?php endif; ?>

 

The above code will show the most viewed posts by tag.

These are the all functions you can use into your theme files in page.php, archive.php, single.php etc.You can also create a custom page template to show the most viewed posts or any of those functions in a single page.

That’s all and let me know if you face any problem implementing the views on your site.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top