Advertisement
stamen4o

AJAX request default

Jun 28th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. function get_latest_posts() {
  2. global $post_request_ajax;
  3.  
  4. $load_time = $_GET['load_time'];
  5. $frontpage = $_GET['frontpage'];
  6. $num_posts = 10; // max amount of posts to load
  7. $number_of_new_posts = 0;
  8. $visible_posts = isset( $_GET['vp'] ) ? (array)$_GET['vp'] : array();
  9.  
  10. query_posts( 'showposts=' . $num_posts . '&post_status=publish' );
  11. ob_start();
  12. while ( have_posts() ) : the_post();
  13. $current_user_id = get_the_author_meta( 'ID' );
  14.  
  15. // Avoid showing the same post if it's already on the page
  16. if ( in_array( get_the_ID(), $visible_posts ) )
  17. continue;
  18.  
  19. // Only show posts with post dates newer than current timestamp
  20. if ( get_gmt_from_date( get_the_time( 'Y-m-d H:i:s' ) ) <= $load_time )
  21. continue;
  22.  
  23. $number_of_new_posts++;
  24. $post_request_ajax = true;
  25.  
  26. p2_load_entry( false );
  27. endwhile;
  28. $posts_html = ob_get_clean();
  29.  
  30. if ( $number_of_new_posts != 0 ) {
  31. nocache_headers();
  32. echo json_encode( array(
  33. 'numberofnewposts' => $number_of_new_posts,
  34. 'html' => $posts_html,
  35. 'lastposttime' => gmdate( 'Y-m-d H:i:s' )
  36. ) );
  37. } else {
  38. nocache_headers();
  39. header( 'HTTP/1.1 200 OK' );
  40. exit;
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement