• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

テンプレートタグ/wp get attachment link

提供: WordPress Codex 日本語版
< テンプレートタグ
2013年6月22日 (土) 18:08時点におけるDjkaz (トーク | 投稿記録)による版 (英語版をコピペ)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

Description

Returns an HTML image element representing an attachment file, if there is any, otherwise an empty string.

Usage

<?php wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); ?>

Default Usage

<?php echo wp_get_attachment_image( 1 ); ?>

If the attachment is an image, the function returns an image at the specified size. For other attachments, the function returns a media icon if the $icon parameter is set to true.

To get attachment IDs dynamically in a template, you can use get_posts('post_type=attachment'), etc.

Parameters

$attachment_id
integer) (必須) ID of the desired attachment.
初期値: なし
$size
string/array) (Optional) Image size. Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32). As of Version 2.5, this parameter does not affect the size of media icons, which are always shown at their original size.
初期値: 'thumbnail'

Instead of using an array which requires checking all of the image sizes, you should consider registering a size with add_image_size so that a cropped version is generated. It's much more efficient than having to find the closest sized image.

$icon
boolean) (Optional) Use a media icon to represent the attachment.
初期値: 'False'
  • 1 (True)
  • 0 (False) - Default
$attr
string/array) (Optional) Query string or array of attributes.
初期値: なし
$default_attr = array(
	'src'	=> $src,
	'class'	=> "attachment-$size",
	'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )),
);

The $attr argument is merged with WordPress's default attributes and passed through the wp_get_attachment_image_attributes filter.

Examples

Display all images as a list

To display all of the images and titles attached to a certain page and display them as a list of bullets you can use the following:

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           echo wp_get_attachment_image( $attachment->ID, 'full' );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';
          }
     }

 endwhile; endif; ?>
</ul>

Return Value

an HTML img element or empty string on failure.

Change Log

Since: 2.5.0

Source File

wp_get_attachment_image() is located in wp-includes/media.php.

Related

添付ファイル関数: get_children(), get attached media(), the_attachment_link(), get_attachment_link(), wp_get_attachment_link(), wp_get_attachment_image(), wp_get_attachment_image_src(), wp_get_attachment_url(), wp_get_attachment_thumb_file(), wp_get_attachment_thumb_url(), is_attachment(), wp_get_attachment_metadata()



関数リファレンステンプレートタグ目次もご覧ください。