Introduction

You are a content creator and create post. Now you need to count post views and display them to viewers in wordpress. The easiest way to count such post views is to incorporate the below-given code in any WordPress theme so that your viewers can easily know the number of views on each blog post on your WordPress website. Using this code, readers can determine the popularity of the article with a higher number of views and they will feel more secure. Hence want to click to view the entire blog post when the number of views is higher than the others. So, in this article, we will lead you to know about how to count post views and display in WordPress without using plugins. 

Step1. Add post views function

Activate your theme. Go to appearance >> Theme Editor. Now in left hand side, find function.php file and add following code in it . This is a function that initiated for counting post views. Here you can use your own function name instead of ‘ function_name_given ‘ . We recommend to use theme prefix as well with function name such as theme_name_function_name_given .

if ( ! function_exists( 'function_name_given' ) ) :    
/**     * get the value of view.     */ 
function function_name_given($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; 

Step2. Add post views count

Now similarly add following code in single.php but before endwhile loop. This code will count the number of views with the help of above function in a visited post.

<?php function_name_given(get_the_ID()); ?>

Step3. Display count post views

After getting count of post views , it should be displayed where ever you like to display using following code such as in index.php or in single.php itself. You can use Div instead of Li as per your requirement in your WordPress website.

<li > <i class="fa fa-eye"></i>                            
<?php                             
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); };                            ?>                             
</li>

Below is the example from pro voice blog wordpress theme.

This feature is available in many of our premium themes. Hence this code is truly and efficiently works.

count post views

Conclusion:-

If you are able to implement above codes in your theme, you will definitely count and display your blog post count without using any plugins. If need support then you can contact us. We will here to support you. Meanwhile you can see some of our cool free themes here.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Click to access the login or register cheeseDo NOT follow this link or you will be banned from the site!