Tutorial 1
Use this code in functions.php
function rm_post_view_count(){ if ( is_single() ){ global $post; $count_post = esc_attr( get_post_meta( $post->ID, '_post_views_count', true) ); if( $count_post == ''){ $count_post = 1; add_post_meta( $post->ID, '_post_views_count', $count_post); }else{ $count_post = (int)$count_post + 1; update_post_meta( $post->ID, '_post_views_count', $count_post); } } } add_action('wp_head', 'rm_post_view_count');
Or use this code in your single.php
if ( is_single() ){ global $post; $count_post = esc_attr( get_post_meta( $post->ID, '_post_views_count', true) ); if( $count_post == ''){ $count_post = 1; add_post_meta( $post->ID, '_post_views_count', $count_post); }else{ $count_post = (int)$count_post + 1; update_post_meta( $post->ID, '_post_views_count', $count_post); } }
then use this code for display post counter
global $post; $visitor_count = get_post_meta( $post->ID, '_post_views_count', true); if( $visitor_count == '' ){ $visitor_count = 0; } if( $visitor_count >= 1000 ){ $visitor_count = round( ($visitor_count/1000), 2 ); $visitor_count = $visitor_count.'k'; } echo esc_attr($visitor_count);
Tutorial 2
Add this code your functions.php
// function to display number of posts. 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 View"; } return $count.' Views'; } // function to count views. 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); } }
Add this code to your single.php file
<?php setPostViews(get_the_ID()); ?>
Add this code for display post counter
<?php echo getPostViews(get_the_ID()); ?>
Muthu Krishnan
July 14, 2017Hi Thanks for the code.it works
Rubel Miah
July 15, 2017Thanks
Mayweather vs. McGregor PPV
August 1, 2017Add this code your functions.php
Charlie Puth Instagram
September 28, 2017thanks for comment!
ady 1
July 23, 2017https://uploads.disquscdn.com/images/18732f413964dfa9b67e337a452c7b00c7c43886b88f598b54191c7637679b0a.jpg
thank u Rubel miah for the code . it nice code and its work with me , but i have some problem and i hope to help me fix it .
how to make this code give time per count view , and stop it to count administrator user .
i found the code that gives time per count post view but this code count administrator user Also.
are there way to make the code do both job .
Rubel Miah
July 24, 2017Hi Ady,
Use this function.
if ( ! is_admin() ) {
CODES
}
Thanks
ady 1
July 24, 2017Hi Rubel miah,
thank u for respond and help.
i use the function u give me , but its still count administrator user .
https://uploads.disquscdn.com/images/6f6f60ae61c8bc2fe019806eeed5d0e4ece6dfd05096885ec0c2486b1db9d2bc.jpg
Rubel Miah
July 24, 2017Add all codes under this function.
ady 1
July 24, 2017Hi Rubel Miah ,
I add all codes under the function,but its still count administrator user.
https://uploads.disquscdn.com/images/da86e26992861fd9e0196b7b40b59b9152843f37d9ac46f57cfba204452b8f67.jpg
ady 1
July 25, 2017Hi Rubel Miah,
thank u very much for help , and I appreciate your response and try to help me, I hope i didn’t bother you with my many questions, After all I suggested to leave it Count.
Thank you, and sorry if I bothered you, I wish you best.
masood
September 7, 2017hi rubel miah. i have to hide views count from post body, because its not showing exact count. let me know the way of hide it.
Rubel Miah
September 8, 2017Hi Masood,
Please give me your website link.
wiloke
September 16, 2017Awesome! But please note that, in case, if the website is using a caching plugin, wp_head will not work, We need to use ajax instead
Rubel Miah
October 24, 2017Thanks. I will do it.
Halil ibrahim YÜCE
October 12, 2017Thanks for this great codes bro, it works good but that`s not counting true, increasing two by two. Why is it increasing two per one visit ?
Rubel Miah
October 24, 2017Hi Halil,
Can you share your website link.
Thanks
Halil ibrahim YÜCE
October 24, 2017Thanks for response bro 🙂
yucetasarim.com/demo/supreme
Rubel Miah
October 24, 2017It’s working perfectly on the single page. Please clean the browser cache then check.
Halil ibrahim YÜCE
October 24, 2017Thanks bro, I cleared browser cache and closed the super cache plugin too. Tested it again but its happening same. When I enter to post its counting 1 view, its OK but when I went to other pages its counting 1 more too ?
Rubel Miah
October 28, 2017You can try “Tutorial 2”, I hope it will worked.
Halil ibrahim YÜCE
October 28, 2017I tried it too but same problem, its counting 2 by 2 🙁 I added codes single.php and other post type pages too. Is it problem ? For example Im adding codes to single-video.php too.
Rubel Miah
October 29, 2017Yes, It’s Problem.
Add this code in functions.php
// function to display number of posts.
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 View”;
}
return $count.’ Views’;
}
// function to count views.
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);
}
}
// function to display number of video posts.
function getVideoPostViews($postID){
$count_key = ‘video_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 View”;
}
return $count.’ Views’;
}
// function to count views.
function setVideoPostViews($postID) {
$count_key = ‘video_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);
}
}
Add this code in single.php
setPostViews(get_the_ID());
And this code in single-video.php page.
setVideoPostViews(get_the_ID());
Add this code in Post meta area.
echo getPostViews(get_the_ID());
And this code in Video post meta area.
echo getVideoPostViews(get_the_ID());
Thanks.
Halil ibrahim YÜCE
October 29, 2017thanks bro but sorry for bothering you, I made this too but same problem continues :/
Dreamelf
December 1, 2017Hello!
I’m sorry about the bad English wording. That was the problem with me. At the post counter, it must be set to zero, and then it counts well, one at a time.
https://uploads.disquscdn.com/images/f04d6125bc21dc06e88f8b27f02762850ad58ca26d988b0c719490110b70e347.jpg
Md Al-Amin Ahmed
October 22, 2017I can’t post. This is my website: https://cybercoderbd.com
Rubel Miah
October 24, 2017Hi, Md Al-Amin,
How can I help you?
Keyur Verma
December 17, 2017nice and good : http://thekipkopmusic.com/
monthly calendar
December 17, 2017+1