how to display post view in wordpress
wordpress
wordpress post view count
wordpress single post view count
wordpress view count without plugin
wp post view
How to Display Wordpress Post View Count in Post or Pages ? Twick Trick
Orbit Design
6:01 AM
How to Display Wordpress Post View Count in Post or Pages ?
You can add wordpress post view count on your pages or posts easily without any plugin. Just you need to paste some php code your theme functions.php file.
So Let's Start...
Step- 01: Add the below code to your theme functions.php file.
//View Count
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "(0)";
}
return $count.'';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
Step-02: Now add below code where you want to display view count in your theme.
<?php setPostViews(get_the_ID()); ?>
<?php echo getPostViews(get_the_ID()); ?>
You are done.
0 Comments