james-riter

About Time POST Count WordPress

Jun 26th, 2013
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. About Time POST Count
  2.  
  3. --- functions.php
  4.  
  5. // Display or Count how many times a post has been viewed.
  6. // id = the post id and action = display or count
  7. function arixWp_PostViews( $id, $action ) {
  8. $axCountMeta = 'ax_post_views';
  9. // Your Custom field that stores the views
  10. $axCount = get_post_meta($id, $axCountMeta, true);
  11. if ( $axCount == '' ) {
  12. if ( $action == 'count' ) {
  13. $axCount = 0; }
  14. delete_post_meta( $id, $axCountMeta );
  15. add_post_meta( $id, $axCountMeta, 0 );
  16.  
  17. if ( $action == 'display' ) { echo '<span class="times-read">Be The First To Read And Share!!</span>'; } }
  18.  
  19. else { if ( $action == 'count' ) {
  20. $axCount++; update_post_meta( $id, $axCountMeta, $axCount );
  21. }
  22. else { echo '<span class="times-read">Has Been Read ' . $axCount . ' Times!</span>';
  23. }
  24. } }
  25.  
  26. //allbloggingtips.com/2011/12/19/add-post-views-counter-in-wordpress-posts-without-plugin/#ixzz2XLmIwM00
  27.  
  28.  
  29. --- single/pages files :
  30.  
  31. Add this as a class (Not in loop) (Gives the ID to read)
  32. <?php the_ID(); ?>
  33.  
  34. Add this after "the_post();" (It counts the IDs)
  35. echo arixWp_PostViews( get_the_ID(), 'count' );
  36.  
  37. Add this out of the loop. . . (It shows the post count)
  38. echo arixWp_PostViews( get_the_ID(), 'display' );
  39.  
  40. --- category and other loops
  41. Add this to display the count after "the_post();"
  42. echo arixWp_PostViews( $post->ID, 'display' );
  43.  
  44.  
  45.  
  46. That should be it. . . Test and retest
Advertisement
Add Comment
Please, Sign In to add comment