skip to Main Content

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


  1. Chosen as BEST ANSWER

    Apparently you need to call addMediaGalleryData() if you want to get the Alt information.

    $items = $block->getProductCollection()->addMediaGalleryData()->getItems();
    foreach($items as $_product){
        $values = ($_product->getMediaAttributeValues());
        $images = $_product->getMediaGalleryImages();
    }
    

    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 the Alt value from there.


  2. 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')

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search