Skip to content

Instantly share code, notes, and snippets.

@JMWebDevelopment
Created December 19, 2016 02:44
Show Gist options
  • Save JMWebDevelopment/5b516207dfc662205b6e4248556ff802 to your computer and use it in GitHub Desktop.
Save JMWebDevelopment/5b516207dfc662205b6e4248556ff802 to your computer and use it in GitHub Desktop.
/**
* Register the necessary fields to display post information correctly
*/
add_action( 'rest_api_init', 'theme_slug_register_rest_fields' );
function theme_slug_register_rest_fields() {
register_rest_field( 'post',
'comments',
array(
'get_callback' => 'theme_slug_get_comments',
'update_callback' => null,
'schema' => null,
) );
}
/**
* Returns the array of comments for a given post
*
* @param $object
*
* @param $field_name
*
* @param $request
*
* @return mixed, list of comments for a post
*/
function theme_slug_get_comments( $object, $field_name, $request ) {
return get_comments( array( 'post_id' => $object[ 'id' ] ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment