【发布时间】:2020-06-29 05:07:32
【问题描述】:
我正在尝试在 wordpress 中制作一个插件,它需要制作一个包含 3 列的表格,该表格将从 api 中提取 json 记录并显示在表格中,但它显示所有 html 标签而不是制作一个 html表,请查看输出图像,告诉我我缺少什么。
<?php
function json_posts()
{
$response = wp_remote_get('API_FOR_JSON_DATA');
if (is_array($response)) {
$header = $response['headers'];
$body = $response['body'];
$array = json_decode( $body, true );
echo '<table><thead><tr><th>Name</th><th>Email</th></tr></thead><tbody>';
foreach ($array as $item) {
echo "<tr><td>".$item['id']."</td><td>".$item['name']."</td><td>".$item['username']."</td></tr>";
}
echo '</tbody></table>';
}
}
add_action('rest_api_init', function () {
register_rest_route('myplugin/v1', 'posts', [
'methods' => 'GET',
'callback' => 'json_posts',
]);
});
【问题讨论】:
-
嗯?你在哪里输出这个?你用什么来测试这个?
-
使用 apache 服务器,并尝试在 chrome __SITE__/wp-json/myplugin/v1/posts 中获取详细信息
-
尝试返回值。我所有的 api 路由都带有 return 。我会在你的第一个回声之前 ob_start() 。并返回 ob_get_clean()
-
你的 json_posts() 之后还有一个额外的 }
-
哪里有多余的}3开{和3关
标签: php html json wordpress api