How To Show Views Count In Every Post – WordPress

Without any plugin (Only using some codes)

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 -

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>

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


Aditya Kumar

Founder, omm

Hi, I am a passionate blogger, love to evaluate new things & share with people.


Find Our More Cool Articles Below
             
Aditya Kumar

Aditya Kumar

Love to explore the tech.

4 thoughts on “How To Show Views Count In Every Post – WordPress

Leave a Reply

Your email address will not be published.