^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_image_tag()用法示例

get_image_tag( int $id,  string $alt,  string $title,  string $align,  string|array $size = 'medium' )

Gets an img tag for an image attachment, scaling it down if requested.


描述

The ‘get_image_tag_class’ filter allows for changing the class name for the image without having to use regular expressions on the HTML content. The parameters are: what WordPress will use for the class, the Attachment ID, image align value, and the size the image should be.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

The second filter, ‘get_image_tag’, has the HTML content, which can then be further manipulated by a plugin to change all attribute values and even HTML content.


参数

$id

(int)
(Required)
Attachment ID.

$alt

(string)
(Required)
Image 描述 for the alt attribute.

$title

(string)
(Required)
Image 描述 for the title attribute.

$align

(string)
(Required)
Part of the class name for aligning the image.

$size

(string|array)
(Optional)
Registered image size to retrieve a tag for. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: ‘medium’


返回值

(string) HTML IMG element for given image attachment


源代码

File: wp-includes/media.php

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 ) . '" ' : '';

	$class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' 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|array $size  Size of image. Image size or array of width and height values (in that order).
	 *                            Default 'medium'.
	 */
	$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );

	$html = '<img src="' . esc_attr($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   Alternate text.
	 * @param string       $title Attachment title.
	 * @param string       $align Part of the class name for aligning the image.
	 * @param string|array $size  Size of image. Image size or array of width and height values (in that order).
	 *                            Default 'medium'.
	 */
	return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

Uses

  • wp-includes/formatting.php:
    esc_attr()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/media.php:
    get_image_tag_class
  • wp-includes/media.php:
    get_image_tag
  • wp-includes/media.php:
    image_downsize()
  • wp-includes/media.php:
    image_hwstring()
  • Show 1 more use


Used By

  • wp-admin/includes/media.php:
    get_image_send_to_editor()

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈