LoginSignup
1
3

More than 5 years have passed since last update.

WordPressの画像取得関数

Last updated at Posted at 2019-03-16

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 ); 
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3