0

I have created one Custom Attribute of media image type.

Attribute name = product_image_custom_1.

I need to display this custom image on Product Page at Footer.

I have used Below code in phtml file to print image.

<?php
        $_helper = $this->helper('Magento\Catalog\Helper\Output');
        $_product = $block->getProduct();

        $productImageAttr = $_product->getCustomAttribute( 'product_image_custom_1' );
        $productImage = $this->helper('Magento\Catalog\Helper\Image')
            ->init($_product, 'product_image_custom_1')
            ->setImageFile($productImageAttr->getValue());
?>
        <img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($productImage->getLabel()) ?>" />

it prints image Fine, But Image label in Alt not display. it prints Product Name.

how to print alt text attribute in frontend?

2

You can use below code

$productImageAttr = $_product->getCustomAttribute( 'product_image_custom_1' );
$productImage = $this->helper('Magento\Catalog\Helper\Image')
    ->init($_product, 'product_image_custom_1')
    ->setImageFile($productImageAttr->getValue());
$mediaGallery = $_product->getData('media_gallery');
foreach($mediaGallery['images'] as $imagesCode)
{
    if($imagesCode['file'] == $productImageAttr->getValue())
    {
        echo $imagesCode['label'];  
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.