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

wordpress函数get_header_image_tag()用法示例

get_header_image_tag( array $attr = array() )

Create image tag markup for a custom header image.


描述


参数

$attr

(array)
(Optional)
Additional attributes for the image tag. Can be used to override the default attributes.

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

Default value: array()


返回值

(string) HTML image element markup or empty string on failure.


源代码

File: wp-includes/theme.php

function get_header_image_tag( $attr = array() ) {
	$header = get_custom_header();
	$header->url = get_header_image();

	if ( ! $header->url ) {
		return '';
	}

	$width = absint( $header->width );
	$height = absint( $header->height );

	$attr = wp_parse_args(
		$attr,
		array(
			'src' => $header->url,
			'width' => $width,
			'height' => $height,
			'alt' => get_bloginfo( 'name' ),
		)
	);

	// Generate 'srcset' and 'sizes' if not already present.
	if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) {
		$image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true );
		$size_array = array( $width, $height );

		if ( is_array( $image_meta ) ) {
			$srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id );
			$sizes = ! empty( $attr['sizes'] ) ? $attr['sizes'] : wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id );

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

	$attr = array_map( 'esc_attr', $attr );
	$html = '<img';

	foreach ( $attr as $name => $value ) {
		$html .= ' ' . $name . '="' . $value . '"';
	}

	$html .= ' />';

	/**
	 * Filters the markup of header images.
	 *
	 * @since 4.4.0
	 *
	 * @param string $html   The HTML image tag markup being filtered.
	 * @param object $header The custom header object returned by 'get_custom_header()'.
	 * @param array  $attr   Array of the attributes for the image tag.
	 */
	return apply_filters( 'get_header_image_tag', $html, $header, $attr );
}

更新日志

Version 描述
4.4.0 Introduced.

相关函数

Uses

  • wp-includes/theme.php:
    get_header_image_tag
  • wp-includes/media.php:
    wp_calculate_image_srcset()
  • wp-includes/media.php:
    wp_calculate_image_sizes()
  • wp-includes/theme.php:
    get_custom_header()
  • wp-includes/theme.php:
    get_header_image()
  • wp-includes/general-template.php:
    get_bloginfo()
  • wp-includes/functions.php:
    absint()
  • wp-includes/functions.php:
    wp_parse_args()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/post.php:
    get_post_meta()
  • Show 5 more uses


Used By

  • wp-includes/theme.php:
    get_custom_header_markup()
  • wp-includes/theme.php:
    the_header_image_tag()

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

热门推荐

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

反馈