I am trying to display a Product Image
but am not able to get the correct data for the Alt Text
$image = $block->getImage($_product, 'category_page_grid');
When I call $image->getLabel()
it is returning the name of the Product
rather than the Alt Text
2
Answers
Apparently you need to call
addMediaGalleryData()
if you want to get theAlt
information.I could not find a way to get the
Alt
value when calling$image = $block->getImage($product, 'category_page_grid')
Though I could loop through the
$images
and reference the image name that matched$image
and get theAlt
value from there.That question has been answered similarly on Stackexchange: https://magento.stackexchange.com/questions/210995/media-image-attribute-width-height-and-alt-tag-in-a-phtml-magento-2
Use this and call
$image->getAttribute('alt')