我正在尝试为我的一个站点创建自定义端点,以便将ACF字段拉入另一个站点。我的主题函数中有以下代码。php
// register the endpoints needed
add_action( \'rest_api_init\', function( ) {
/*
// post types to include
$_pts = array( \'posts\', );
// loop the post types and create a rest endpoint for the ACF fields for them
foreach ( $_pts as $_pt ) {
// var_dump($_pt . \'/acf\');
// register the rest endpoint
register_rest_route( \'kp-api/v1\', $_pt . \'/acf/(?P<id>\\d+)\', array(
\'methods\' => array( \'GET\', ),
\'get_callback\' => function( $data ) {
return \'HERE\';
},
\'args\' => array(
\'id\' => array( \'validate_callback\' => function( $param, $request, $key ) {
return is_numeric( $param );
} ),
),
) );
}
*/
// register the rest endpoint
register_rest_route( \'kp-api/v1\', \'/TEST/(?P<id>\\d+)\', array(
\'methods\' => array( \'GET\', ),
\'get_callback\' => function( $data ) {
return \'HERE\';
},
\'args\' => array(
\'id\' => array( \'validate_callback\' => function( $param, $request, $key ) {
return is_numeric( $param );
} ),
),
) );
} );
当我尝试浏览我的端点时,我最终得到
{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
而且,我在站点的/wp-json/中看不到端点。然而,我可以看到,至少名称空间本身确实是通过浏览创建的:/wp-json/kp-api/v1
但是,其中没有端点。
{
"namespace": "kp-api/v1",
"routes": {
"/kp-api/v1": {
"namespace": "kp-api/v1",
"methods": [
"GET"
],
"endpoints": [
{
"methods": [
"GET"
],
"args": {
"namespace": {
"required": false,
"default": "kp-api/v1"
},
"context": {
"required": false,
"default": "view"
}
}
}
],
"_links": {
"self": "https://example.com/wp-json/kp-api/v1"
}
}
},
"_links": {
"up": [
{
"href": "https://example.com/wp-json/"
}
]
}
}
我做错了什么?不,我没有使用wp json插件,只是使用了正常的内置Wordpress功能(v.5.3.2)