wp_get_attachment_image()通过id获取缩略图(特色图像)的图片信息

目录

描述

译文

存在图片时,返回代表附件文件的HTML图片标签。否则返回空字符串。

概要
(字符) $image = function wp_get_attachment_image($attachment_id, $size='thumbnail', $icon = false);

原文

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

wp_get_attachment_image() 描述

用法

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

wp_get_attachment_image() 用法

参数

$attachment_id

(integer) (必填) ID of the desired attachment.

默认值: None

$size

(string/array) (可选) 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) (可选) Use a media icon to represent the attachment.

默认值: 'False'

  • 1 (True)
  • 0 (False) - Default

$attr

(string/array) (可选) Query string or array of attributes.

默认值: None

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$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.

wp_get_attachment_image() 参数

示例

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:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<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>

wp_get_attachment_image() 示例

历史

添加于 版本: 2.5.0

wp_get_attachment_image() 历史

源文件

wp_get_attachment_image() 函数的代码位于 wp-includes/media.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Get an HTML img element representing an image attachment
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|array $size          Optional. Registered image size or flat array of height and width
 *                                    dimensions. Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
 * @param string|array $attr          Optional. Attributes for the image markup. Default empty.
 * @return string HTML img element or empty string on failure.
 */
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",
			'alt'	=> trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
		);
		if ( empty($default_attr['alt']) )
			$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
		if ( empty($default_attr['alt']) )
			$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title

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

		/**
		 * Filter 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.
		 */
		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
		$attr = array_map( 'esc_attr', $attr );
		$html = rtrim(" $value ) {
			$html .= " $name=" . '"' . $value . '"';
		}
		$html .= ' />';
	}

	return $html;
}

wp_get_attachment_image() 源文件

相关