TransWikia.com

Magento 2.3.5-p - Show product page > product image alt and title tag (replaced fotorama gallery)

Magento Asked on December 14, 2021

I’m using the following code to replace the fotorama image gallery on the product page based on this article.

appdesignfrontendMypackagemythemeMagento_Catalogtemplatesproductviewgallery.phtml

<?php

$product = $block->getProduct();
$imageHelper = $this->helper('MagentoCatalogHelperImage');

if (!$product->getImage() || $product->getImage() == 'no_selection') {
    $image = $imageHelper->getDefaultPlaceholderUrl('image');
} else {
    $image = $imageHelper->init($product, 'product_page_image_medium')
            ->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
            ->setImageFile($product->getImage())
            ->getUrl();
}
?>
<div class="gallery-placeholder">
    <img src="<?php echo $image; ?>" >
</div>

However the alt tag isn’t formatted correctly, it shows as "thisisanexampleimage" instead of "This is an example image" as defined under that product

Image and Video >> Image detail

How can I get the alt tag to show a properly formatted alt tag and also include a title tag.

One Answer

I figured it out, the code that works as follows.

Added alt="<?php echo $product->getName(); ?>

<?php

$product = $block->getProduct();
$imageHelper = $this->helper('MagentoCatalogHelperImage');

if (!$product->getImage() || $product->getImage() == 'no_selection') {
    $image = $imageHelper->getDefaultPlaceholderUrl('image');
} else {
    $image = $imageHelper->init($product, 'product_page_image_medium')
            ->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
            ->setImageFile($product->getImage())
            ->getUrl();
}
?>
<div class="gallery-placeholder">
    <img src="<?php echo $image; ?>" alt="<?php echo $product->getName(); ?>" >
</div>

Answered by user2240778 on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP