How To Show Views Count In Every Post – WordPress 2020 Latest


Why to show view count ? 😯

Well! for a new visitor on your blog or site views count makes it easy to decide whether he is on right article to get his / her problems solved.

So if you also want to show views count on your wordpress site to attract more visitors just follow our detailed guide in easy steps below.


Views count Post views

STEPS To Show Views Count

1) Firstly, go to the Functions.php file of the currently used theme of WordPress. Then copy the code below and paste it (at the end usually) before the closing tag?> of the Functions.php file.

function gt_get_post_view() {
    $count = get_post_meta( get_the_ID(), 'post_views_count', true );
    return "$count views";
}
function gt_set_post_view() {
    $key = 'post_views_count';
    $post_id = get_the_ID();
    $count = (int) get_post_meta( $post_id, $key, true );
    $count++;
    update_post_meta( $post_id, $key, $count );
}
function gt_posts_column_views( $columns ) {
    $columns['post_views'] = 'Views';
    return $columns;
}
function gt_posts_custom_column_views( $column ) {
    if ( $column === 'post_views') {
        echo gt_get_post_view();
    }
}
add_filter( 'manage_posts_columns', 'gt_posts_column_views' );
add_action( 'manage_posts_custom_column', 'gt_posts_custom_column_views' );

2) Then, copy the code below and paste it into single.php file in the while loop.

<?php gt_set_post_view(); ?>

find the image below for reference -
Views count Post views

3) Next, copy the following code and paste it where you want to show the number of views.

<?php gt_get_post_view(); ?>

4) Now, Paste the below code in function.php file under INC folder i.e. “inc/function.php”, See the below pictures to know where to place the code in “function.php” file.

<span class=”views_custom” style =”font-size:12px; color:#888888;”><i class=”fa fa-eye”></i>&nbsp;&nbsp;<span class=”views_custom2″ > <?php echo gt_get_post_view(); ?></span></span>

Views count Post views

4. When it’s all done, you will see the post view like this:


Final Thoughts

Hope this article helped you in adding view count in your wordpress blog / site, If you liked the post please share with your friends from button below. If you have any query or need help please feel free to ask in comments.

Leave a Reply