我正在跟踪this 创建WP-API自定义端点的教程。
我总是在击球时出错/wp-json/custom-plugin/v2/get-all-post-ids/ 在…上postman 要测试:
{
"code": "rest_no_route",
"message": "No route was found matching
the URL and request method",
"data":
{
"status": 404
}
}
我已经创建了一个自定义插件。php文件位于/plugins/custom-plugin/directory。
<?php
if ( ! defined( \'ABSPATH\' ) ) exit;
add_action( \'rest_api_init\', \'dt_register_api_hooks\' );
function dt_register_api_hooks() {
register_rest_route( \'custom-plugin/v2\', \'/get-all-post-ids/\', array(
\'methods\' => \'GET\',
\'callback\' => \'dt_get_all_post_ids\',
)
);
}
// Return all post IDs
function dt_get_all_post_ids() {
if ( false === ( $all_post_ids = get_transient( \'dt_all_post_ids\' ) ) ) {
$all_post_ids = get_posts( array(
\'numberposts\' => -1,
\'post_type\' => \'post\',
\'fields\' => \'ids\',
) );
// cache for 2 hours
set_transient( \'dt_all_post_ids\', $all_post_ids, 60*60*2 );
}
return $all_post_ids;
}
?>
请指导我如何解决此问题。
谢谢