【发布时间】:2018-12-10 09:28:56
【问题描述】:
我在 wordpress 中创建了一个名为 custom_users 的自定义表,我想通过 API 访问记录
functions.php
function get_wp_custom_users()
{
global $wpdb;
$custom_users = $wpdb->get_results("SELECT * FROM wp_custom_users");
foreach ($custom_users as $custom_user)
{
echo $custom_user->first_name.' '.$custom_user->last_name;
}
}
add_action('rest_api_init', function()
{
register_rest_route( 'wpcustomusers/v1', '/users/', array(
'methods' => 'GET',
'callback' => 'get_wp_custom_users'
));
});
API可以通过urlhttp://localhost/mywebsite/wp-json/wpcustomusers/v1/users访问
您知道如何从 API 请求将 for loop 中的结果作为 JSON 返回吗?
我希望看到所有的字段都返回了。。谢谢你的帮助。
【问题讨论】:
-
将循环替换为
echo json_encode($custom_users);...?
标签: javascript php html css wordpress