aThemeArt

How to Count Post Views & Display Without WordPress Plugins.

Do you want to know how to count post views in WordPress without using plugins?

We all create content on our site, but we are always unaware of those post views. There are so many plugins available in the WordPress repository. Yet, using plugins for simple things is not recommended.

So, today we are here with a step-by-step tutorial on how to count post views in WordPress without using plugins.

Let’s get started!

Why Post Views Count Necessary?

After lots of effort and time, we prepare a POST. We post it, and as always, there comes a question in our mind, “Has anyone viewed it?”, “No comments in my post. Can people see it or not?”

We, as content creators, are always curious about our post views. We have also seen in some posts, the total number of views is visible. It helps to show the popularity of the post and, yes, of course, counts the visitor’s engagement too.

The numbers not only give you excitement to write more, but sometimes it might disappoint you as well. Rather than getting disappointed, we should try to learn about what went wrong, and the post is not getting views.

Hence, displaying the post views works as a tool for maintaining the quality of your posts.

Thus, we are here today to show you the easiest way to count POST views & display them without using plugins.

Without using plugins, you can add a few codes in your theme function file, and “BOOM,” the magic happens. But if you are not sure about how to play with all those nuts and bolts in your WordPress files, then we suggest you follow the steps very carefully.

Here we go:

Step 1: Injecting into the function file

Messing up with your function file can sometimes be dangerous. So, be sure about what you are doing, and it’s always a good idea to make a backup of your core files before messing up with them.

At this stage, I assume that you have your theme activated. If so then

Click on “Appearance” on your WordPress dashboard.

Go to “Theme Editor”

Navigate and find “function.php” on the left-hand side.

Add the following code into it.

We recommend you for adding the code at the end of that file. Because of that, the code will not get messed with the other codes, and your site will not get down.

if ( ! function_exists( 'function_post_count' ) ) : 
/** * get the value of view. */ 
function function_post_count($postID) { 
$count_key = 'wpb_post_views_count'; 
$count = get_post_meta($postID, $count_key, true); 
if($count ==''){ 
$count = 1; 
delete_post_meta($postID, $count_key); 
add_post_meta($postID, $count_key, '1'); 
} else { $count++; 
update_post_meta($postID,
 $count_key, $count); 
}
}
endif; 

Here we added a function and triggering the function for counting the post views.

The function name is not mandatory as ‘function_post_count’; if you don’t like my naming style, you can change it to your choices.

We recommend you to use theme prefix as the initials for the function names as theme_name_function_post_count, but as like a function name, you are free to call it by the name of your choice.

Click on Update to save your changes.

Step 2: Adding POST views counter

As in the previous step, look for the “single.php” file and add the below-given code. Make sure you add the codes just before the “End While” loop.

 function_post_count(get_the_ID());  

This piece of code will get the count of the number of views in your post for you with the help of the function that we have created in step 1.

Click Update to save your changes.

Step 3: Finally, displaying the count views

After implementing the function and triggering it, we have the count of the post views in our hands. Now, we want to display, and believe me; this is not rocket science!

You have various options to choose either to display the count inside the div or on a li. Any one of these HTML tags that fit in your design is acceptable.

For displaying the number, use the code from below. Here I have used

  • and an icon for my design, but the choice is entirely yours.

     
    
    if ( get_post_meta( get_the_ID() , 'wpb_post_views_count', true) == '') { echo '0' ; 
    } else { 
    echo get_post_meta( get_the_ID() , 'wpb_post_views_count', true); }; 
    
    
    

    Now you save all your tweaks and then reload your page to check. Here you go. Now you have made the visibility of your POST views count.

    Conclusion:

    Finally, if you have followed the steps correctly, you’ll display the POST count views in your post.

    As we already mentioned earlier, the count plays an essential role in maintaining your site’s popularity and boosting your POST’s quality. Because you get to know that people like your piece of work. Higher the number means people seem it interesting in your work and they are expecting more from you.

    Wrapping up

    If the steps don’t work for you, you have messed up with your files, but don’t worry, you can write us back about it or directly contact us. We are there for you to look at your back. Hence, without any hesitation, give it a try and let us know.

    Inspire us with your love!

    FacebookTwitterReddit
  • Avatar

    Art of Awesome Professional WordPress themes & Site templates. Our responsive themes are designed to work seamlessly for all desktop and mobile devices. Modern, Clean & Easy To Use.

    You can check also