Last active
March 24, 2017 17:11
-
-
Save westonruter/5a0e8606c9fa1aa2b709eaef90bdb33b to your computer and use it in GitHub Desktop.
This file contains 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 | |
// Make internal REST API requests for all resources the client JS app will need. | |
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); | |
$request->set_query_params( array( | |
'per_page' => 5, | |
'orderby' => 'title', | |
'order' => 'desc', | |
) ); | |
$response = rest_get_server()->dispatch( $request ); | |
$response = apply_filters( 'rest_post_dispatch', $response, rest_get_server(), $request ); | |
$posts = rest_get_server()->response_to_data( $response, true /* _embed */ ); | |
?> | |
<script> | |
wp.api.init().done( function() { | |
var posts = new wp.api.collections.Posts(); | |
posts.reset( <?php echo wp_json_encode( $posts ) ?> ); | |
// App is now ready to render immediately without having to wait for fetching. | |
} ); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment