Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'rest_api_init', 'slug_register_pagination' );
- function slug_register_pagination() {
- register_rest_field( 'post',
- 'pagination',
- array(
- 'get_callback' => 'get_pagination_in_json',
- 'update_callback' => null,
- 'schema' => null,
- )
- );
- }
- function get_pagination_in_json($post, $field_name, $request) {
- // Ensure global post is correctly set
- $old_post = $GLOBALS['post'];
- $GLOBALS['post'] = get_post($post['id']);
- $post = $GLOBALS['post'];
- setup_postdata($post);
- $previous_post = get_adjacent_post(true, '', true, 'category' );
- $next_post = get_adjacent_post(true, '', false, 'category' );
- if ( is_a( $previous_post, 'WP_Post' ) ) {
- $post_response['previous'] = $previous_post->post_name;
- }
- if ( is_a( $next_post, 'WP_Post' ) ) {
- $post_response['next'] = $next_post->post_name;
- }
- // Reset global post to its old value
- $GLOBALS['post'] = $old_post;
- return $post_response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement