嗨,我正在我的项目上建立WordPress rest API的自定义端点。我正在使用以下代码从网站获取单个响应。
function get_single_post( $request ) {
$response = array();
$args = [
\'id\' => $request[\'id\'],
\'post_type\' => \'payment\',
];
$post = get_posts($args);
$metas = get_post_meta($request[\'id\'],\'\');
$post_id = $post->ID;
$response[$post_id][\'response\'] = $post[0];
$response[$post_id][\'metas\'] = $metas;
return $response;
}
add_action(\'rest_api_init\', function(){
register_rest_route(\'booking/v1\', \'posts\',[
\'methods\'=> \'GET\',
\'callback\' => \'booking_posts\',
]);
register_rest_route(\'booking/v1\',\'/posts/(?P<id>\\d+)\',[
\'methods\' => \'GET\',
// Register the callback for the endpoint.
\'callback\' => \'get_single_post\',
]);
});
我没有收到回复中的帖子。请告知我这里做错了什么?
以下是我的回答:
{
"": {
"response": {
"ID": 12042,
"post_author": "10",
"post_date": "2020-06-12 15:15:42",
"post_date_gmt": "2020-06-12 14:15:42",
"post_content": "",
"post_title": "xxxxxxxxxxxxxxx",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "xxxxxxxxxxx",
"to_ping": "",
"pinged": "",
"post_modified": "2020-06-12 15:15:42",
"post_modified_gmt": "2020-06-12 14:15:42",
"post_content_filtered": "",
"post_parent": 0,
"guid": "https://xxxxxxxx/payment/xxxxxxxxxx/",
"menu_order": 0,
"post_type": "payment",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
},
"metas": {
"chauffeur_payment_status": [
"Unpaid"
],
"chauffeur_payment_num_passengers": [
"1"
],
"chauffeur_payment_num_bags": [
"0"
],
"chauffeur_payment_first_name": [
"xxxxxxx"
],
"chauffeur_payment_last_name": [
"xxxxxxxx"
],
"chauffeur_payment_email": [
"xxxxxxxxx"
],
"chauffeur_payment_phone_num": [
"xxxxxx"
],
"chauffeur_payment_flight_number": [
""
],
"chauffeur_payment_additional_info": [
""
],
"chauffeur_payment_pickup_address": [
"xxxxxxx"
],
"chauffeur_payment_dropoff_address": [
"xxxxxxxx"
],
"chauffeur_payment_pickup_date": [
"12/06/2020"
],
"chauffeur_payment_pickup_time": [
"12:00"
],
"chauffeur_payment_trip_distance": [
"3,907 km"
],
"chauffeur_payment_trip_time": [
"1 day 12 hours"
],
"chauffeur_payment_item_name": [
"Standard-size taxi"
],
"chauffeur_payment_trip_type": [
"one_way"
],
"chauffeur_payment_return_journey": [
"One Way"
],
"chauffeur_payment_num_hours": [
""
],
"chauffeur_payment_amount": [
"xxxx"
],
"chauffeur_payment_full_pickup_address": [
""
],
"chauffeur_payment_pickup_instructions": [
""
],
"chauffeur_payment_full_dropoff_address": [
""
],
"chauffeur_payment_dropoff_instructions": [
""
]
}
}
}
响应的id->;ID应与posts/相同。事实并非如此。