2017年06月14日 WordPressphp

1.WP REST APIプラグインをインストール

管理画面から「WordPress REST API」プラグインをインストールし、有効化します。

これで
http://◯◯◯.com/wp-json/wp/v2/posts
にアクセスすれば、記事の情報がjson形式で表示されます。 WordPress REST API

jsonを取得・表示させる

<?php

//WordPressのURLを指定(新着記事2件取得)
$url = 'http://◯◯◯.com/wp-json/wp/v2/posts?_embed&per_page=2';


$ch = curl_init(); // 1. 初期化
curl_setopt( $ch, CURLOPT_URL, $url ); // 2. オプションを設定
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec( $ch ); // 3. 実行してデータを得る
curl_close(); // 4. 終了


//連想配列にする
$arr_json = json_decode($result,true);
  // print_r($arr_json);


     foreach ($arr_json as $json) {
$json_date = $json["date"];
       $json_date = date('Y年n月j日', strtotime($json_date));
$json_link = $json["link"];
       $json_title = $json["title"]["rendered"];
$json_img = $json["_embedded"]["wp:featuredmedia"][0]["media_details"]["sizes"]["full"]["source_url"];
?>

      <div class="detail">
        <?php //アイキャッチがある場合
            if($json_img) { ?>
        <div class="blog_img">
          <img src="<?= $json_img; ?>" class="article_title_img">
        </div>
        <?php } ?>

        <strong><span class="date">
        <?= $json_date; ?>
        </span></strong><br>
        <a href="<?= $json_link; ?>"> <span class="listTitle">
        <?= $json_title; ?>
        </span> </a> </div>
 
      <?php
      			}
?>

この投稿へのコメント

コメントはまだありません。

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます。
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CAPTCHA


ピックアップ記事

advanced custom fieldの値を更新

2020年02月10日 WordPress
[php] update_field( 'フィールド名', '値', $post_id );[/php] 参考 : https://developers.wano.co.jp/1373/
「advanced custom fieldの値を更新」をはてなブックマークに追加

未解決 twitter関連のOAuth認証が通らない 

2011年05月27日 WordPress
WP to Twitterなど、twitter関連のOAuth認証が通らない 原因は不明だけど、サーバのcURLやら外部向けのHTTPS通信が許可されてないやら、その辺っぽい。 ちなみに現在使っているのは、お名前.comの共用サーバです。 VPSの方も契約してるんで、そっちに移行した方が早いかも。 http://bono.s201.xrea.com/pib/wpm/?p=32 twitt…
「未解決 twitter関連のOAuth認証が通らない 」をはてなブックマークに追加

ページIDからリンクを設定

2011年11月28日 WordPress
[php] <a href="<?php echo get_permalink('75'); ?>">お問い合わせ</a> [/php]
「ページIDからリンクを設定」をはてなブックマークに追加

WordPressで特定の親カテゴリとその子カテゴリに所属する場合の条件分岐の方法

2022年11月12日 WordPress
functions.phpに以下を記述 [php]if ( ! function_exists( 'post_is_in_descendant_category' ) ) { function post_is_in_descendant_category( $cats, $_post = null ) { foreach ( (array) $cats as $c…
「WordPressで特定の親カテゴリとその子カテゴリに所属する場合の条件分岐の方法」をはてなブックマークに追加
© graffiti on the web . All rights reserved. WordPress Theme by comfy