MENU

getEntityRecordで画像を取得する

getEntityRecordで画像を取得する方法です。2種類の方法があります。

1つ目の方法は、1番目の引数に"postType"、2番目の引数に"attachment"、3番目の引数には取得したい画像のIDを指定します。

const media = useSelect((select) => {
  const data = select("core").getEntityRecord("postType", "attachment", post?.meta.icon);
  return data;
}, [post]);
console.log(media);

2つ目の方法は、1番目の引数に"root"、2番目の引数に"media"、3番目の引数には取得したい画像のIDを指定します。

const media = useSelect((select) => {
  const data = select("core").getEntityRecord("root", "media", post?.meta.icon);
  return data;
}, [post]);
console.log(media);

参考:
https://developer.wordpress.org/block-editor/reference-guides/data/data-core/#getentityrecord
How to get featured_media of a post with getEntityRecords?