【问题标题】:Wordpress Remove Lazy Load from Main Product ImageWordpress 从主要产品图像中删除延迟加载
【发布时间】:2022-11-15 06:52:28
【问题描述】:

需要防止产品页面的主图延迟加载。

主要产品图片加载在“woocommerce/single-product/product-image.php”中

它使用: wp_get_attachment_image( $attachment_id, $size, $icon, $attr );获取图像。

在上面的函数内部,有:

// Add `loading` attribute.
    if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
        $default_attr['loading'] = wp_get_loading_attr_default( 'wp_get_attachment_image' );
    }

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

    // If the default value of `lazy` for the `loading` attribute is overridden
    // to omit the attribute for this image, ensure it is not included.
    if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) {
        unset( $attr['loading'] );
    }

很明显,可以不延迟加载它,但我只是不完全明白我怎么能做到这一点?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可能能够做到这一点的一种方法是使用 wp_get_attachment_image_attributes 过滤器,如下所示:

    <?php
    
    add_filter("wp_get_attachment_image_attributes", function($attr, $attachment, $size) {
        $first_image_post_id = 'ID of the image you want to remove the lazy attr';
        
        if ( $attachment->ID === $first_image_post_id ) {
            unset( $attr['loading'] );
        }
    
        return $attr;
    }, 10, 3);
    

    有关 WP 文档的更多信息:https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-22
      • 2018-03-22
      • 1970-01-01
      • 2019-09-27
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多