Advertisement
pusatdata

Menampilkan Post View tanpa Plugin

Jun 5th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. http://www.wp-tutorials.com/how-to-count-post-views-without-plugin/
  2.  
  3. Paste di functions.php:
  4.  
  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. function setPostViews($postID) {
  16. $count_key = 'post_views_count';
  17. $count = get_post_meta($postID, $count_key, true);
  18. if($count==''){
  19. $count = 0;
  20. delete_post_meta($postID, $count_key);
  21. add_post_meta($postID, $count_key, '0');
  22. }else{
  23. $count++;
  24. update_post_meta($postID, $count_key, $count);
  25. }
  26. }
  27. // Remove issues with prefetching adding extra views
  28. remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
  29.  
  30.  
  31. ============================================
  32.  
  33. Jika ditampilkan di LOOP, kodenya:
  34. setPostViews(get_the_ID());
  35.  
  36. ============================================
  37. Atau di post-single, pages, dll, kodenya:
  38. <?php echo getPostViews(get_the_ID()); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement