Skip to content

Instantly share code, notes, and snippets.

@uncatcrea
Last active August 29, 2015 14:22
Show Gist options
  • Save uncatcrea/2c8bd079db00bd0f43b0 to your computer and use it in GitHub Desktop.
Save uncatcrea/2c8bd079db00bd0f43b0 to your computer and use it in GitHub Desktop.
By default WP-AppKit doesn't get post terms to keep web service responses light. But thanks to the wpak_post_data, you can customize the returned data.
/*
* Applies to: since WP-AppKit 0.1
* Goal: Use wpak_post_data to add post terms to the web service response and display them in app
You may want to look at https://codex.wordpress.org/Function_Reference/wp_get_post_terms
* Usage: WordPress: in the app's theme PHP folder or a separate plugin.
* App: single.html or archive.html
*/
add_filter( 'wpak_post_data', 'add_terms_to_my_app_posts', 10, 3 );
function add_terms_to_my_app_posts ( $post_data, $post, $component ) {
// Get post terms
$terms = wp_get_post_terms( $post->ID, 'my_taxonomy' );
// Don't overload the web service response
// Pick only the needed variables
// In this example: name and description of terms
$terms_in_app = array();
foreach( $terms as $term ) {
$terms_in_app[] = array( 'name' => $term->name, 'description' => $term->description);
}
$post_data['my_terms'] = $terms_in_app;
return $post_data; //Return the modified $post_data
}
<div>
<% for (i=0; i < post.my_terms.length; i++) { %>
<div> <%= post.my_terms[i]['name'] %> </div>
<div> <%= post.my_terms[i]['description'] %> </div>
<% } %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment