Last active
April 18, 2016 07:12
-
-
Save ville6000/0c090ee23567ce1737af to your computer and use it in GitHub Desktop.
Add siblings field to WP REST API data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'rest_api_init', 'slug_register_siglings' ); | |
function slug_register_siglings() { | |
register_api_field( 'post', | |
'siblings', | |
array( | |
'get_callback' => 'slug_get_siglings', | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
} | |
function slug_get_siglings( $object, $field_name, $request ) { | |
$next_post = get_adjacent_post( false, '', false ); | |
$prev_post = get_adjacent_post( false, '', true ); | |
return array( | |
'next' => $next_post->ID, | |
'next_post_title' => $next_post->post_title, | |
'prev' => $prev_post->ID, | |
'prev_post_title' => $prev_post->post_title, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment