WP-API有钩子,比如
rest_prepare_page
以增强端点。对于每种帖子类型,给它不同的挂钩,下面的代码示例将标签添加到帖子和页面,并说明它是如何工作的。
/**
* Add properties to posts and pages endpoints.
*/function wp_api_theming_posts_properties( $response ) {
// Set author\'s name.
$author = get_userdata( $response->data[\'author\'] );
$response->data[\'author\'] = array( \'id\' => $author->ID, \'link\' => get_author_posts_url( $author->ID ), \'name\' => $author->data->display_name, );
// Add post classes.
$response->data[\'post_class\'] = get_post_class( \'\', $response->data[\'id\'] );
// Add categories.
$categories = wp_api_theming_get_post_terms( $response->data[\'id\'], \'category\' );
if ( ! empty( $categories ) ) {
$response->data[\'categories\'] = $categories;
}
// Add tags. $tags = wp_api_theming_get_post_terms( $response->data, \'post_tag\' );
if ( ! empty( $tags ) ) {
$response->data[\'tags\'] = $tags; } return $response;
}
add_filter( \'rest_prepare_post\', \'wp_api_theming_posts_properties\' );
add_filter( \'rest_prepare_page\', \'wp_api_theming_posts_properties\' );
/** * Get a post\'s terms with archive links. */
function wp_api_theming_get_post_terms( $id = false, $taxonomy = \'category\' ) {
// We need an ID for this one.
if ( ! $id ) {
return FALSE;
}
// Validate the taxonomy argument.
$valid_tax = apply_filters( \'wp_api_theming_valid_tax\', array( \'category\', \'post_tag\' ) );
$taxonomy = ( in_array( $taxonomy, $valid_tax ) ) ? $taxonomy : \'category\';
// Fetch our terms.
$terms = wp_get_post_terms( absint( $id ), $taxonomy );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
// Append a link property to each term.
foreach ( $terms as $term ) {
$link = get_term_link( $term );
$term->link = $link;
}
}
return $terms;
}
另请参阅我的小插件,以减少不同点的API结果。我认为了解如何改变、提高结果和终点也很有帮助。
https://github.com/bueltge/WP-REST-API-Filter-Items