If you have a WordPress blog and you want to displayย Post View Countsย in every post and do not want to use any third-party plugin. How to do so?
So, let’s see how we can do it inside. Before that make sure you are familiar with the framework and PHP.
The simple way is to integrate the blog with the view-checking feature, so you will know the number of post views on each blog post page on your WordPress site. Using this feature, readers can determine the quality of the article thanks to the number of views and they will feel more secure and want to click on the blog post to read when the number of views is higher than the others.
Display Post View Counts In WordPress Without Plugin
Step 1:
Go to your function.php file inside the theme folder. I prefer any changes should make inside the child theme.
Copy and paste the below code:
function display_post_view() {
$count = get_post_meta( get_the_ID(), 'post_views_count', true );
return "$count views"; // you can add icon if you want to
// return '<i class="fas fa-eye"></i>'. $count;
}
function display_post_view_count() {
$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 display_post_view();
}
}
add_filter( 'manage_posts_columns', 'gt_posts_column_views' );
add_action( 'manage_posts_custom_column', 'gt_posts_custom_column_views' );
Step 2:
Go to your single.php file inside the theme folder and add the below code inside the While Loop
<?php display_post_view(); ?>
Step 3:
Now finally it’s time to display the post view count on every post. If you are familiar with WordPress then you can add wherever you want to display or you simply add to your single.php file inside your theme folder.
<?= display_post_view_count(); ?>
or
<?php echo display_post_view_count(); ?>
So, that’s it now you can see your blog post view counts on every blog post page.