How To Display Page View Count In WordPress

display page view count

There are a couple of tricks available to enhance the visitor’s engagement in your website. One of the ways is to display page view count in WordPress. It doesn’t only show a specific number but also creates an eagerness to read the post on your page. 

If you yet don’t know how to display page view count in WordPress, learn it from this post. In this post, I am going to show you the whole process. After acquiring the process apply it to your website and increase the engagement. 

How To Display Page View Count In WordPress

You can display page view count in Two different ways. You can either display via plugins or via functions.php or custom coding. 

Display Page View Count Using Plugin

This is the most easiest and recommended way. At first install the Post View Counter plugin from the WordPress plugins directory. Don’t forget to activate it after you download it. 

Now navigate to settings -> Post View Counter. From here customize the settings as you need. And if you like your counter in the top position, toggle the display tab and select position as ‘before the content’.

You are done. In this way, you can easily display page view count. 

Display Page View Count Using Custom Coding

In this procedure, you have to write some code in your themes function.php file. First, navigate to Appearance -> Theme Editor and select functions.php(Theme Functions)

Now insert the following code before the closing tag and click on Update File.

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);
    }
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Now open single.php(Single Post) and paste the following code in the while loop.

setPostViews(get_the_ID());

Now the last step, copy and paste the following code where you want to display the number of page views. 

echo getPostViews(get_the_ID());

Now your post should look like this.

Conclusion

Hope you have done it correctly to display your page view count in the WordPress site of yours. To minimize problems it is recommended to use plugins instead of custom codes. If you liked the post please share it with your friends and don’t forget to share this post with your friends.

close

Easy WP Tweaks

10X Your Site Growth!

Join the growing library of Easy WP Tweaks to grow your WordPress site.

Leave a Comment

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

Copy link
Powered by Social Snap