get_image_tag

函数
get_image_tag ( $id, $alt, $title, $align, $size = 'medium' )
参数
  • (int) $id Attachment ID.
    Required:
  • (string) $alt Image description for the alt attribute.
    Required:
  • (string) $title Image description for the title attribute.
    Required:
  • (string) $align Part of the class name for aligning the image.
    Required:
  • (string|int[]) $size Optional. Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'medium'.
    Required:
    Default: 'medium'
返回值
  • (string) HTML IMG element for given image attachment?
定义位置
相关方法
get_header_image_tagget_the_tagsget_tagget_file_dataget_site_meta
引入
2.5.0
弃用
-

为图片附件获取一个img标签,如果要求的话,可以将其缩小。

{@see ‘get_image_tag_class’}过滤器允许改变图片的类别名称,而不必在HTML内容上使用正则表达式。参数是:WordPress将使用的类,附件ID,图像对齐值,以及图像的大小。

第二个过滤器,{@see ‘get_image_tag’},有HTML内容,然后可以通过插件进一步操作,改变所有属性值,甚至HTML内容。

function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {

	list( $img_src, $width, $height ) = image_downsize( $id, $size );
	$hwstring                         = image_hwstring( $width, $height );

	$title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';

	$size_class = is_array( $size ) ? implode( 'x', $size ) : $size;
	$class      = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size_class ) . ' wp-image-' . $id;

	/**
	 * Filters the value of the attachment's image tag class attribute.
	 *
	 * @since 2.6.0
	 *
	 * @param string       $class CSS class name or space-separated list of classes.
	 * @param int          $id    Attachment ID.
	 * @param string       $align Part of the class name for aligning the image.
	 * @param string|int[] $size  Requested image size. Can be any registered image size name, or
	 *                            an array of width and height values in pixels (in that order).
	 */
	$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );

	$html = '<img src="' . esc_url( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />';

	/**
	 * Filters the HTML content for the image tag.
	 *
	 * @since 2.6.0
	 *
	 * @param string       $html  HTML content for the image.
	 * @param int          $id    Attachment ID.
	 * @param string       $alt   Image description for the alt attribute.
	 * @param string       $title Image description for the title attribute.
	 * @param string       $align Part of the class name for aligning the image.
	 * @param string|int[] $size  Requested image size. Can be any registered image size name, or
	 *                            an array of width and height values in pixels (in that order).
	 */
	return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
}

常见问题

FAQs
查看更多 >