◆使用プラグイン
・Custom Post Type UI(カスタム投稿)
・Advanced Custom Fields(カスタムフィールド)
・The Repeater Field(リピーターフィールド)
◆出来ないこと
WP REST APIを使用して、リピーターフィールドの情報をJSON形式で取得したいのですが、
取得できませんでした。
リピーターフィールドを使用していないカスタムフィールドは問題なく取得できるので、
リピータフィールドを取得する記述が間違っているのだと考えられます。
わかる方いましたら教えてほしいです。
◆やりたい事
「slide_image」というカスタムフィールドの情報をWP REST APIでJSON形式で返ってくるようにしたいです。
◆カスタム投稿の設定
投稿タイプスラッグ 「photo」
◆カスタムフィールドの設定
フィールド名
slide_images
フィールドタイプ
Repeater
└ サブフィールド名
└ slide_image
└ フィールドタイプ
└ image
└ サブフィールド名
└ slide_caption
└ フィールドタイプ
└ text
◆記述したプログラム
add_action( 'rest_api_init', 'slug_register_onsen' ); function slug_register_onsen() { register_rest_field( 'photo', 'data', array( 'get_callback' => function( $object, $field_name, $request ) { $meta_fields = array( 'slide_images', 'slide_image', 'slide_caption' ); $meta = array(); foreach ( $meta_fields as $field ) { $meta[ $field ] = get_post_meta( $object[ 'id' ], $field, true ); } return $meta; }, 'update_callback' => null, 'schema' => null, ) ); }
◆アクセスしたリンク
https://example.jp/wp-json/wp/v2/photo
◆JSON形式で返された結果
[ { "id": 355, "date": "2018-09-28T17:50:18", "date_gmt": "2018-09-28T08:50:18", "guid": { "rendered": "https://example.jp/?post_type=photo&p=355" }, "modified": "2018-10-04T17:28:52", "modified_gmt": "2018-10-04T08:28:52", "slug": "%e6%96%87%e5%ad%97%e6%95%b0%e7%a2%ba%e8%aa%8d%e6%96%87%e5%ad%97%e6%95%b0%e7%a2%ba%e8%aa%8d%e6%96%87%e5%ad%97%e6%95%b0%e7%a2%ba%e8%aa%8d%e6%96%87%e5%ad%97%e6%95%b0%e7%a2%ba%e8%aa%8d%e6%96%87%e5%ad%97", "status": "publish", "type": "photo", "link": "https://example.jp/photo/%e6%96%87%e5%ad%97%e6%95%b0%e7%a2%ba%e8%aa%8d%e6%96%87%e5%ad%97%e6%95%b0%e7%a2%ba%e8%aa%8d%e6%96%87%e5%ad%97%e6%95%b0%e7%a2%ba%e8%aa%8d%e6%96%87%e5%ad%97%e6%95%b0%e7%a2%ba%e8%aa%8d%e6%96%87%e5%ad%97/", "title": { "rendered": "文字数確認文字数確認文字数確認文字数確認文字数確認文字数確" }, "template": "", "data": { "slide_images": "1", "slide_image": "", "slide_caption": "" }, "_links": { "self": [ { "href": "https://example.jp/wp-json/wp/v2/photo/355" } ], "collection": [ { "href": "https://example.jp/wp-json/wp/v2/photo" } ], "about": [ { "href": "https://example.jp/wp-json/wp/v2/types/photo" } ], "wp:attachment": [ { "href": "https://example.jp/wp-json/wp/v2/media?parent=355" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } } ]
回答2件
あなたの回答
tips
プレビュー