Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ********************************** How To Add a Post Counter in blog post **************************
- //First Paste this code in functions.php file to register post counter
- /*This Code is for Post Counter*/
- 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 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);
- }
- }
- ****************************************************************************
- //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
- <?php setPostViews(get_the_ID()); ?>
- **************************************************************************
- //Than use this code where you want to show post count number
- <?php echo getPostViews(get_the_ID()); ?>
- ********************************************** Thanks ********************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement