返回介绍

wp_get_attachment_image()

发布于 2017-09-11 11:58:59 字数 7705 浏览 946 评论 0 收藏 0

wp_get_attachment_image( int $attachment_id,  string|array $size = 'thumbnail',  bool $icon = false,  string|array $attr = '' )

Get an HTML img element representing an image attachment


description

While $size will accept an array, it is better to register 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 and then having the browser scale down the image.


参数

$attachment_id

(int) (Required) Image attachment ID.

$size

(string|array) (Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: 'thumbnail'

$icon

(bool) (Optional) Whether the image should be treated as an icon.

Default value: false

$attr

(string|array) (Optional) Attributes for the image markup.

Default value: ''


返回值

(string) HTML img element or empty string on failure.


源代码

File: wp-includes/media.php

function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {
	$html = '';
	$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
	if ( $image ) {
		list($src, $width, $height) = $image;
		$hwstring = image_hwstring($width, $height);
		$size_class = $size;
		if ( is_array( $size_class ) ) {
			$size_class = join( 'x', $size_class );
		}
		$attachment = get_post($attachment_id);
		$default_attr = array(
			'src'	=> $src,
			'class'	=> "attachment-$size_class size-$size_class",
			'alt'	=> trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
		);

		$attr = wp_parse_args( $attr, $default_attr );

		// Generate 'srcset' and 'sizes' if not already present.
		if ( empty( $attr['srcset'] ) ) {
			$image_meta = wp_get_attachment_metadata( $attachment_id );

			if ( is_array( $image_meta ) ) {
				$size_array = array( absint( $width ), absint( $height ) );
				$srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
				$sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );

				if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
					$attr['srcset'] = $srcset;

					if ( empty( $attr['sizes'] ) ) {
$attr['sizes'] = $sizes;
					}
				}
			}
		}

		/**
		 * Filters the list of attachment image attributes.
		 *
		 * @since 2.8.0
		 *
		 * @param array        $attr       Attributes for the image markup.
		 * @param WP_Post      $attachment Image attachment post.
		 * @param string|array $size       Requested size. Image size or array of width and height values
		 *                                 (in that order). Default 'thumbnail'.
		 */
		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
		$attr = array_map( 'esc_attr', $attr );
		$html = rtrim("<img $hwstring");
		foreach ( $attr as $name => $value ) {
			$html .= " $name=" . '"' . $value . '"';
		}
		$html .= ' />';
	}

	return $html;
}

更新日志

Versiondescription
2.5.0Introduced.

More Information

Usage


wp_get_attachment_image( $attachment_id, $size, $icon, $attr );

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( array( 'post_type' => 'attachment' ) ), etc.


相关函数

Uses

  • wp-includes/media.php: wp_calculate_image_srcset()
  • wp-includes/media.php: wp_calculate_image_sizes()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/functions.php: absint()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/media.php: wp_get_attachment_image_src()
  • wp-includes/media.php: wp_get_attachment_image_attributes
  • wp-includes/media.php: image_hwstring()
  • wp-includes/post.php: wp_get_attachment_metadata()
  • wp-includes/post.php: get_post_meta()
  • wp-includes/post.php: get_post()
  • Show 6 more uses Hide more uses

Used By

  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::render_media()
  • wp-includes/general-template.php: get_custom_logo()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_title()
  • wp-admin/includes/post.php: _wp_post_thumbnail_html()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_response()
  • wp-includes/post-thumbnail-template.php: get_the_post_thumbnail()
  • wp-includes/post-template.php: wp_get_attachment_link()
  • wp-includes/media.php: gallery_shortcode()
  • wp-includes/post.php: set_post_thumbnail()
  • Show 4 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 7You must log in to vote on the helpfulness of this note Contributed by Rinku Y

    wp_get_attachment_image function can accept four values as you can see:

    wp_get_attachment_image ( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )

    So i always use:

    <?php echo wp_get_attachment_image( get_the_ID(), array('700', '600'), "", array( "class" => "img-responsive" ) );  ?>

    Note: we can simply use get_the_ID() to pass id of active post. and here 700 is width and 600 is height of attachment image. we can also pass our class as array( “class” => “img-responsive” ).

  2. 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();
    
    		$attachments = get_posts( array(
    			'post_type'   => 'attachment',
    			'numberposts' => -1,
    			'post_status' => null,
    			'post_parent' => $post->ID
    		) );
    		
    		if ( $attachments ) {
    			foreach ( $attachments as $attachment ) {
    				?>
    				<li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
    					<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
    				</li>
    				<?php
    			}
    		}
    	endwhile; endif; ?>
    </ul>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文