Created
June 12, 2013 03:13
-
-
Save dsebao/5762592 to your computer and use it in GitHub Desktop.
Simple visit counting in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //In function.php | |
| function get_post_views($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 set_post_views($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); | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //IN single.php put this | |
| ?> | |
| <?php set_post_views(get_the_ID());?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment