php [WordPress] [WP Rocket] Wrapper功能:将WP Rocket的LazyLoad应用于wp_get_attachment_image()

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [WordPress] [WP Rocket] Wrapper功能:将WP Rocket的LazyLoad应用于wp_get_attachment_image()相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Wrapper function: Applies WP Rocket’s LazyLoad to wp_get_attachment_image()
 *
 * @link https://developer.wordpress.org/reference/functions/wp_get_attachment_image/
 * @link https://github.com/wp-media/wp-rocket/blob/v2.10.9/inc/front/lazyload.php#L24-L47
 * 
 * @param  int           $attachment_id  (Required) Image attachment ID.
 * @param  string|array  $size           (Optional) Image size. Accepts any 
 *                                       valid image size, or an array of width 
 *                                       and height values in pixels (in that order).
 * @param  boolean       $icon           (Optional) Whether the image should be 
 *                                       treated as an icon.
 * @param  string|array  $attr           Attributes for the image markup.
 * @return string                        HTML img element or empty string on failure.
 */
function wp_rocket__wp_get_attachment_image__lazyload( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {

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

	if( function_exists( 'rocket_lazyload_images' ) ) {
		return rocket_lazyload_images( $image_html );
	}

	return $image_html;
}

以上是关于php [WordPress] [WP Rocket] Wrapper功能:将WP Rocket的LazyLoad应用于wp_get_attachment_image()的主要内容,如果未能解决你的问题,请参考以下文章