個別記事のデータを取得した場合には、
http://www.sample.jp/blog/wp-json/posts/
にその記事のIDをくっ付けてやるだけです。
例:http://www.sample.jp/blog/wp-json/posts/記事のID/
コードは以下の様になります。
<?php $path = "http://www.sample.jp/blog/wp-json/posts/記事のID/"; $ch = curl_init($path); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($ch); curl_close($ch); //パース $post = json_decode($json, true); if($post === NULL) { //パースに失敗した時は処理終了 return; }else{ $date = strftime("%Y 年 %m 月 %d 日", strtotime(substr($post["date"], 0, 10))); print<<<EOF <div>タイトル:{$post["title"]}</div> <div>{$post["content"]}</div> <div>公開日時:{$date}</div> EOF; } ?>
この「記事のID」の部分を動的に書き換えるようにすれば、
記事一覧から記事個別ページへのリンクを貼って、
ブログの構築を行えるようになります。
この記事へのコメントはありません。