[Resolved] Add HTML atrributes to featured images on all posts

Home Forums Support [Resolved] Add HTML atrributes to featured images on all posts

Home Forums Support Add HTML atrributes to featured images on all posts

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #369811
    David

    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

    #369937
    Tom
    Lead Developer
    Lead Developer

    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;
    }
    #369982
    David

    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

    #370038
    Tom
    Lead Developer
    Lead Developer

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

    Glad it works 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.