WordPress.org

Ready to get started?Download WordPress

Forums

gallery loop (3 posts)

  1. charlcfc
    Member
    Posted 1 year ago #

    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

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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;
    
    }
  3. charlcfc
    Member
    Posted 1 year ago #

    Thank you very much keesiemeijer. That worked 100%! :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags