Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. add_action( 'rest_api_init', 'slug_register_pagination' );
  2.  
  3. function slug_register_pagination() {
  4. register_rest_field( 'post',
  5. 'pagination',
  6. array(
  7. 'get_callback' => 'get_pagination_in_json',
  8. 'update_callback' => null,
  9. 'schema' => null,
  10. )
  11. );
  12. }
  13.  
  14. function get_pagination_in_json($post, $field_name, $request) {
  15.  
  16. // Ensure global post is correctly set
  17. $old_post = $GLOBALS['post'];
  18. $GLOBALS['post'] = get_post($post['id']);
  19. $post = $GLOBALS['post'];
  20. setup_postdata($post);
  21.  
  22. $previous_post = get_adjacent_post(true, '', true, 'category' );
  23. $next_post = get_adjacent_post(true, '', false, 'category' );
  24.  
  25. if ( is_a( $previous_post, 'WP_Post' ) ) {
  26. $post_response['previous'] = $previous_post->post_name;
  27. }
  28.  
  29. if ( is_a( $next_post, 'WP_Post' ) ) {
  30. $post_response['next'] = $next_post->post_name;
  31. }
  32.  
  33. // Reset global post to its old value
  34. $GLOBALS['post'] = $old_post;
  35.  
  36. return $post_response;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement