• Hi all

    I’m using the standard WP gallery functionality & would like to modify the WP gallery loop to include the id of each attachment image.

    Default:

    <img width=”150″ height=”150″ title=”imagetitle” alt=”imagealt” class=”attachment-thumbnail” src=”source-to-image-jpg”>

    Would like add the image id:

    <img width=”150″ height=”150″ title=”imagetitle” alt=”imagealt” id=”#id” class=”attachment-thumbnail” src=”source-to-image-jpg”>

    Is there a filter which a person can use for this or any other code to achieve this?

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    WordPress uses wp_get_attachment_link to display the gallery images. You can filter that function to add the ID. Example:

    add_filter( 'wp_get_attachment_link', 'add_image_attribute_id',10,6);
    function add_image_attribute_id($markup, $id, $size, $permalink, $icon, $text ) {
    
    	$pos = strpos($markup, '<img ');
    	if ($pos !== false) {
    		return substr_replace($markup, '<img id="image-' . $id . '" ', $pos, strlen('<img '));
    	}
    
    	return $markup;
    
    }

    Thread Starter charlcfc

    (@charlcfc)

    Thank you very much keesiemeijer. That worked 100%! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘gallery loop’ is closed to new replies.