get_the_image and custom attributes

Get The Image 3 posts 2 voices

  1. Hi Justin,

    I am looking to add few custom attributes to my images, like data-something="yes" etc

    I have looked at get_the_image extension, and I am not sure if I can do this.

    But the_post_thumbnail & get_the_post_thumbnail accepts attr param

    so can you please let me know how I can achieve it with **get_the_image** function as well?

  2. You can’t alter the HTML directly. However, you can have get_the_image() return you an array of image attributes like so:

    $image = get_the_image( array( 'echo' => false, 'format' => 'array' ) );

    You’ll get an array that looks something like this:

    	'src'    => 'some-image.png',
    	'class'  => 'some-class',
    	'alt'    => 'alt text',
    	'width'  => '100',
    	'height' => '100'

    Then, you can use that to build your own image HTML to your liking.

    I created a ticket on GitHub for this feature though:

  3. Thanks, yes I am aware I can get array instead of html, but I am interested in #20, hopefully I will be using it soon 🙂