Last active
February 18, 2016 04:17
-
-
Save yoren/58e8ace753b6544d06b5 to your computer and use it in GitHub Desktop.
Upgrading Your AngularJS Theme To Work With WP API V2
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 | |
function my_rest_prepare_post( $data, $post, $request ) { | |
$_data = $data->data; | |
$thumbnail_id = get_post_thumbnail_id( $post->ID ); | |
$thumbnail = wp_get_attachment_image_src( $thumbnail_id ); | |
$_data['featured_image_thumbnail_url'] = $thumbnail[0]; | |
$data->data = $_data; | |
return $data; | |
} | |
add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 ); |
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 | |
function my_rest_prepare_attachment( $data, $post, $request ) { | |
$_data = $data->data; | |
if ( 'image' == $_data['media_type'] ) | |
$_data['is_image'] = true; | |
else | |
$_data['is_image'] = false; | |
$data->data = $_data; | |
return $data; | |
} | |
add_filter( 'rest_prepare_attachment', 'my_rest_prepare_attachment', 10, 3 ); |
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 | |
function my_rest_post_query( $args, $request ) { | |
if ( isset( $request['filter'] ) && isset( $request['filter']['posts_per_page'] ) && ! empty( $request['filter']['posts_per_page'] ) ) { | |
if ( $request['filter']['posts_per_page'] > 0 ) { | |
$request['per_page'] = $request['filter']['posts_per_page']; | |
} else { | |
$count_query = new WP_Query(); | |
unset( $query_args['paged'] ); | |
$query_result = $count_query->query( $query_args ); | |
$total_posts = $query_result->found_posts; | |
$request['per_page'] = $total_posts; | |
} | |
} | |
return $args; | |
} | |
add_filter( 'rest_post_query', 'my_rest_post_query', 10, 2 ); |
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
<!-- ... --> | |
<ul> | |
<li ng-repeat="post in data.posts"> | |
<a href="blog/{{post.id}}" ng-bind-html="post.title.rendered"></a> | |
<a href="blog/{{post.id}}" ng-if="post.featured_image_thumbnail_url"><img ng-src="{{post.featured_image_thumbnail_url}}" alt="{{post.featured_image.title.rendered}}" /></a> | |
<div ng-bind-html="post.excerpt.rendered"></div> | |
</li> | |
</ul> | |
<!-- ... --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For more info, please refer to my original post: Upgrading Your AngularJS Theme To Work With WP API V2.