WordPressの画像取得についていろいろ種類があり、
いつも混合して覚えているので毎回毎回毎回調べるので自分用まとめ
アイキャッチ画像のURLを取得
$image_url = get_the_post_thumbnail_url( $post, $size );
アイキャッチ画像のIDを取得
$image_id = get_post_thumbnail_id();
アイキャッチ画像を取得( imgタグごと )
$attr = array(
'src' => $src, // アイキャッチ画像の URL
'class' => "attachment-$size", // 指定した大きさ
'alt' => trim( strip_tags( $attachment->post_excerpt ) ), // アイキャッチ画像の抜粋
'title' => trim( strip_tags( $attachment->post_title ) ), // アイキャッチ画像のタイトル
);
$image = get_the_post_thumbnail( $post_id, $size, $attr );
// 値で取得しない場合は the_post_thumbnail()
指定したidのメディア画像を取得( imgタグごと )
$attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
);
$image = wp_get_attachment_image( $attachment_id, $size, $icon, $attr );
指定したidのメディア画像URLを取得
$image_src = wp_get_attachment_image_src( $attachment_id, $size, $icon );