2022年04月29日

WP関数:wp_get_attachment_image_src()

この関数の説明

WP関数の wp_get_attachment_image_src() は、アイキャッチ画像IDからメディアの画像情報を配列で取得する関数です。

 


関数

wp_get_attachment_image_src( $attachment_id, $size )

 


パラメータ

$attachment_id整数必須

アイキャッチ画像のIDを指定します

 

$size(文字列/配列)(オプション)

取得したい画像情報を指定します。

'thumbnail', 'medium', 'large', 'full'が指定できます。

指定しなかった場合は 'thumbnail'の情報が返されます。

 

'full':フル画像です。

'thumbnail', 'medium', 'large':画像が無い場合はフル画像の情報が返されます。

 


戻り値

下記の配列を返します。

[0] => url

[1] => width

[2] => height

[3] => 真偽値: リサイズされいている場合は true、元のサイズの場合は false

 

指定した[$attachment_id]が存在しない場合は下記のデータを返します

あなたのURL/wp-includes/images/media/default.png

 


事例

①アイキャッチ画像をクリックするとフル画像を表示する。

下記の画像をクリックして下さい。画像が拡大表示されます。

上記は下記のプログラムで作成されています。

<?php 
    $thumbnail = get_the_post_thumbnail(); // アイキャッチ画像を取得 
    $img_id = get_post_thumbnail_id(); // アイキャッチ画像のIDを取得する 
    $img = wp_get_attachment_image_src($img_id , 'full'); // フル画像のデータを取得する
 ?> 
<a href='<?php echo esc_url($img[0]);?>'> <?php echo $thumbnail;?> </a>

■2行目:get_the_post_thumbnail() でアイキャッチ画像を取得

■3行目:get_post_thumbnail_id() でアイキャッチ画像のIDを取得

■4行目:wp_get_attachment_image_src()で、メディアライブラリからフル画像データの情報を取得

■6行目:アイキャッチ画像を表示しリンク先をメディアライブラリのフル画像にしている。

 


関連情報

関連関数は アイキャッチ関連関数 を参照してください.

 

 

関数一覧
  • 1.WPのエスケープ処理
  • 2.WPプログラムの基本
  • 3.ヘッダ/フッタ/サイドバで使われる関数
  • 4.DBからの読込関数
  • 5.一般テンプレートの中で使われる関数
  • 6.その他のWP関数
  • add_shortcode()
  • bloginfo()
  • body_class()
  • date_i18n()
  • dynamic_sidebar()
  • edit_post_link()
  • esc_attr()
  • esc_html_e()
  • esc_html()
  • esc_url()
  • get_ancestors()
  • get_cat_ID()
  • get_cat_name()
  • get_category_link()
  • get_category_parents()
  • get_category()
  • get_footer()
  • get_header()
  • get_home_url()
  • get_post_format()
  • get_post_meta()
  • get_post_thumbnail_id()
  • get_query_var()
  • get_search_query()
  • get_sidebar()
  • get_site_url()
  • get_stylesheet_directory_uri()
  • get_stylesheet_uri()
  • get_template_directory_uri()
  • get_template_part()
  • get_the_category()
  • get_the_content()
  • get_the_date()
  • get_the_excerpt()
  • get_the_ID()
  • get_the_modified_date()
  • get_the_modified_time()
  • get_the_permalink()
  • get_the_post_thumbnail()
  • get_the_tags()
  • get_the_time()
  • get_the_title()
  • get_uploaded_header_images()
  • get_year_link()
  • has_category()
  • has_custom_logo()
  • has_header_image()
  • has_header_video()
  • has_nav_menu()
  • has_post_thumbnail()
  • has_tag()
  • header_image()
  • home_url()
  • is_admin()
  • is_category()
  • is_date()
  • is_front_page()
  • is_home()
  • is_main_query()
  • is_page()
  • is_search()
  • is_single()
  • language_attributes()
  • the_category()
  • the_content()
  • the_custom_header_markup()
  • the_custom_logo()
  • the_date()
  • the_excerpt()
  • the_ID()
  • the_modified_date()
  • the_modified_time()
  • the_permalink()
  • the_post_thumbnail()
  • the_search_query()
  • the_tags()
  • the_time()
  • the_title()
  • update_post_meta()
  • wp_get_attachment_image_src()
  • wp_nav_menu()
  • WP_Query()