cancel
Showing results for 
Search instead for 
Did you mean: 

Need Help Retrieving Image with Custom Attribute on Product Page

Need Help Retrieving Image with Custom Attribute on Product Page

Hello everyone,

 

I'm new to Magento and hoping someone with more Magento experience can help me.

 

I'm working with Magento 2.1.1, and on the product page, I've created a new Featured Image attribute.  What I'd like to do is display this image on the product page separately from the main gallery.

 

But after more than a day of trying, I can't make it work.

 

 

So far, I've created a new block in the catalog_product_view.xml file calling a new feature.phtml template.  I'm unsure which block class to use, I've tried a few, but I've tried Magento\Catalog\Block\Product\Image and Magento\Catalog\Block\Product\View\Gallery.

 

Inside the feature.phtml file I'm currently using this code, which is the closest thing I've been able to find online, but it isn't displaying anything.  I've tried other snippets as well to no avail.

 

<?php
$productImageAttr = $product->getCustomAttribute( 'featured_image' );
$productImage = $this->helper('Magento\Catalog\Helper\Image')
    ->init($product, 'featured_image')
    ->setImageFile($productImageAttr->getValue());
?>

<img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" />

If anyone can help me figure out how to make this work, I'd really appreciate it, as I'm really struggling trying to figure this out.

 

Thanks in advance for your help.