Advertisement
shakil97bd

How To Add a Post Counter in blog post

Feb 5th, 2015
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | None | 0 0
  1. ********************************** How To Add a Post Counter in blog post **************************
  2.  
  3. //First Paste this code in functions.php file to register post counter
  4.  
  5.  
  6. /*This Code  is for Post Counter*/
  7. function getPostViews($postID){
  8.     $count_key = 'post_views_count';
  9.     $count = get_post_meta($postID, $count_key, true);
  10.     if($count==''){
  11.         delete_post_meta($postID, $count_key);
  12.         add_post_meta($postID, $count_key, '0');
  13.         return "0 View";
  14.     }
  15.     return $count.' Views';
  16. }
  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. ****************************************************************************
  32.  
  33. //Than paste this code in single.php file to get post view id.You can paste this code anywhere in the post loop of single.php file
  34.  
  35.  
  36.                 <?php setPostViews(get_the_ID()); ?>
  37.  
  38.  
  39.  
  40. **************************************************************************
  41.  
  42. //Than use this code where you want to show post count number
  43.  
  44.         <?php echo getPostViews(get_the_ID()); ?>
  45.        
  46.        
  47. ********************************************** Thanks ********************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement