を作成して、wordpress
の投稿のデータをjson
に生成しようとしています。以下のコードはのfunctions.phpファイルである:それは新しくwordpress
インストールで非常によく動作しますワードプレスで残りのAPIを作成するときにヘッダーエラーを修正できません
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();
}
。私は私のオンラインのウェブサイトでそれを実装する場合でも、それは私にエラーを与える
警告:ヘッダー情報を変更することはできません - すでに によって送られたヘッダを(出力は で開始/ HOME1/public_htmlの/ブログサイト/ WP-コンテンツ/テーマ/ webtheme /header.php:10)
header.php
の行ライン10で159
に /home1/public_html/website-theme/wp-content/themes/webtheme/functions.php では、wp_head()
json
データのみを生成するには、このエラーに乗る方法がわかりません。
ありがとうございました。しかし、私はこのフックを使用すると、私は任意のjsonデータを返しません –
私は簡単な例で私のメッセージを更新します。 – Bazaim
私は 'http:// localhost:1234'というURLにあなたの答えに従っています。これはjsonデータの代わりにページ全体の内容を返します。 –