【发布时间】:2017-02-21 14:11:13
【问题描述】:
我正在尝试创建一个REST API,它为wordpress 帖子生成json 数据。下面的代码在 functions.php 文件中:
add_action('wp_enqueue_scripts', 'create_json_data', 0);
function create_json_data(){
ob_start();
if (isset($_GET['getjson']) && $_GET['getjson'] == true) {
if (have_posts()) {
$data = array();
while (have_posts()) {
the_post();
$new_data = array(
"title" => get_the_title(),
"content" => get_the_excerpt(),
"image" => get_the_post_thumbnail_url(),
"hyperlink" => get_the_permalink()
);
array_push($data, $new_data);
}
}
header('Content-type: application/json');
echo json_encode($data);
exit();
}
ob_end_flush();
}
在新的wordpress 安装中运行良好。但是当我在我的在线网站中实现它时,它给了我错误
警告:无法修改标头信息 - 标头已由 (输出开始于 /home1/public_html/blogsite/wp-content/themes/webtheme/header.php:10) 在 /home1/public_html/website-theme/wp-content/themes/webtheme/functions.php 在第 159 行
header.php 的第 10 行是 wp_head()
我不知道如何解决这个错误,只生成json 数据。
【问题讨论】: