Advertisement
firoze

post view count

Apr 15th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. // post view
  2.  
  3. // put this code into functions.php
  4. // function to display number of posts.
  5. function getPostViews($postID){
  6. $count_key = 'post_views_count';
  7. $count = get_post_meta($postID, $count_key, true);
  8. if($count==''){
  9. delete_post_meta($postID, $count_key);
  10. add_post_meta($postID, $count_key, '0');
  11. return "0 View";
  12. }
  13. return $count.' Views';
  14. }
  15.  
  16. // function to count views.
  17. function setPostViews($postID) {
  18. $count_key = 'post_views_count';
  19. $count = get_post_meta($postID, $count_key, true);
  20. if($count==''){
  21. $count = 0;
  22. delete_post_meta($postID, $count_key);
  23. add_post_meta($postID, $count_key, '0');
  24. }else{
  25. $count++;
  26. update_post_meta($postID, $count_key, $count);
  27. }
  28. }
  29.  
  30.  
  31. // Add it to a column in WP-Admin
  32. add_filter('manage_posts_columns', 'posts_column_views');
  33. add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
  34. function posts_column_views($defaults){
  35. $defaults['post_views'] = __('Views');
  36. return $defaults;
  37. }
  38. function posts_custom_column_views($column_name, $id){
  39. if($column_name === 'post_views'){
  40. echo getPostViews(get_the_ID());
  41. }
  42. }
  43.  
  44. ----------------------------------------------------------------------------------------------------------------------
  45.  
  46. // where count will display paste this code there (mainly post-loop.php & single.php)
  47.  
  48. <?php echo (int) get_post_meta(get_the_ID(), 'post_views_count', true) . ' View(s)';?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement