自定义 WordPress Rest API 搜索端点
WordPress REST API搜索端点
** endpoint =/wp-json/wp/v2/search?search = term **
是否可以为此WordPress REST搜索端点编辑返回对象?我正在尝试将特色图像添加到返回的邮政对象中。
下面的示例是您将如何编辑常规帖子对象,但这不会影响搜索端点返回的对象。
add_action( 'rest_api_init', function () {
register_rest_field( 'post', 'featured_image_src', array(
'get_callback' => function ( $post_arr ) {
$image_src_arr = wp_get_attachment_image_src( $post_arr\['featured_media'\], 'medium' );
return $image_src_arr[0];
},
'update_callback' => null,
'schema' => null
) );
} );
我已经尝试更改... register_rest_field('post'.... to ... register_rest_field('search'...但这不会更改任何内容,因为搜索不是对象类型。
添加& emp; 但没有包含特色的图像。
返回邮政对象,
Wordpress Rest API Search Endpoint
** Endpoint = /wp-json/wp/v2/search?search=term **
Is it possible to edit the return object for the results of this wordpress rest search endpoint? I am trying to add featured image to the post object that is returned.
Example below is how you would edit the regular post object, but this does not effect the object returned by the search endpoint.
add_action( 'rest_api_init', function () {
register_rest_field( 'post', 'featured_image_src', array(
'get_callback' => function ( $post_arr ) {
$image_src_arr = wp_get_attachment_image_src( $post_arr\['featured_media'\], 'medium' );
return $image_src_arr[0];
},
'update_callback' => null,
'schema' => null
) );
} );
I've tried changing ...register_rest_field( 'post'.... to ...register_rest_field( 'search'... but this does not change anything as search is not an object type.
Adding &embed to the url returns the post object but not with the featured image included. The embedded array includes "self" and that is where the other post object data is but wpfeatruedimage is not there, only media ID.
Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)