Site logo

Archived topic

Add HTML atrributes to featured images on all posts

3 replies · Started by David on August 20, 2017

Viewing posts 1–4 of 4

Hi team,

I'd like to add the attribute data-no-lazy="1" to all featured images on my blog posts. What is the best way to do that with GeneratePress?

Thanks
David

Hi David,

Give this a try:

add_filter( 'wp_get_attachment_image_attributes', 'tu_data_attr_featured_image', 10, 2 );
function tu_data_attr_featured_image( $attr, $attachment ) {
    remove_filter( 'wp_get_attachment_image_attributes', 'tu_data_attr_featured_image' );
    $attr['data-no-lazy'] = '1';
    return $attr;
}

Thanks, Tom this works great!

I added this in a GP Hooks. Is there any drawback versus adding this code in functions.php?

Thanks
David

Hooks are primarily meant for visual elements. I would definitely use your functions.php file, or a plugin like Code Snippets.

Glad it works :)

This archived topic is closed to new replies.