Site logo

Archived topic

LCP issue, featured image, srcset & scaled images

20 replies · Started by Margaret on July 17, 2020

Viewing posts 16–21 of 21

Yes, I readded the code just now. Thanks!

add_filter( 'wp_get_attachment_image_attributes', function( $attr ) {
    $attr['class'] .= ' skip-lazy';

    return $attr;
} );

Hi there,

Try this instead:

add_filter( 'wp_get_attachment_image_attributes', function( $attr, $attachment ) {
    if ( $attachment->ID === get_post_thumbnail_id() ) {
        $attr['class'] .= ' skip-lazy';
    }

    return $attr;
}, 10, 2 );

Thank you, that snippet worked perfectly! Last question on this topic - I just realized, I should probably add the "skip-lazy" class to the logo because that image is above the fold as well. How could I add the skip-lazy class to the logo? Could I edit the existing PHP snippet or add another?

Try adding this PHP Snippet:

// Add skip-lazy class to mobile logo
add_filter( 'generate_mobile_header_logo_output', function( $output ) {
  if ( ! function_exists( 'generate_menu_plus_get_defaults' ) ) {
      return $output;
  }

  $settings = wp_parse_args(
      get_option( 'generate_menu_plus_settings', array() ),
      generate_menu_plus_get_defaults()
  );

  return sprintf(
      '<div class="site-logo mobile-header-logo">
          <a href="%1$s" title="%2$s" rel="home">
              <img src="%3$s" class="skip-lazy" alt="%4$s" />
          </a>
      </div>',
      esc_url( apply_filters( 'generate_logo_href' , home_url( '/' ) ) ),
      esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ),
      esc_url( apply_filters( 'generate_mobile_header_logo', $settings['mobile_header_logo'] ) ),
      esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) )
  );
} );

// Add skip-lazy class to navigation logo
add_filter( 'generate_logo_attributes', function( $attr ) {
  $attr['class'] .= ' skip-lazy';

  return $attr;
} );

Thank you!

You're welcome

This archived topic is closed to new replies.