Magento 2 question here:

I've made a custom attribute (media type) with the name of 'bike_range_image', and tried googling and searching in the core and everything I could to get the image with a custom size and such, but can't find the right approach.

I can get it as text, but obviously that's not enough, so this won't cut it:

$productImage = $product->getCustomAttribute( 'bike_range_image' );

Now, some examples in themes that are out there suggest I do this:

$this->helper('Magento\Catalog\Helper\Image')->init($product, 'bike_range_image');

But this gives me an enormous Magento\Catalog\Helper\Image object that doesn't seem to have the right data. (I've got the right product if you're wondering).

Does anyone have some pointers for me?

| improve this question | | | | |

I guess you want to do it in .phtml file. Try this way

$productImageAttr = $product->getCustomAttribute( 'bike_range_image' );
$productImage = $this->helper('Magento\Catalog\Helper\Image')
    ->init($product, 'bike_range_image')

and then in img tag

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

The image size can be defined in xml file, for example view.xml

    <images module="Magento_Catalog">
        <image id="bike_range_image" type="thumbnail">
| improve this answer | | | | |
  • It was a while ago, but yes, ended up doing it like this. I can't vote it up yet, so please someone else do it! – TommyK Jul 15 '16 at 15:10
  • 1
    $product->getCustomAttribute('bike_range_image') is NULL in catalog category pages when $product is loaded from $block->getLoadedProductCollection() – LucScu Oct 18 '17 at 9:33

Use the following code:

$value = $_product->getCustomImage();
if(isset($value) && $value != 'no_selection'):
     $imageUrl = Mage::getHelper('catalog/image')->init($_product,'custom_image');
      echo "<img src='".$imageUrl."' />";
| improve this answer | | | | |
  • That is Magento 1 code, but still I managed to get it like that: $value = $product->getBikeRangeImage(); However, this is not working yet: $value = $product->getBikeRangeImage(); if( isset($value) && $value != 'no_selection' ): echo $this->helper( 'Magento\Catalog\Helper\Image' )->init( $product, 'bike_range_image' )->resize( 310 ); endif; That gives me an error saying it returns an object rather than an url as it should. Any idea? – TommyK Mar 14 '16 at 14:51
  • can anyone shed a light to how I can call an image for a drop-down attribute? Thank you – roger Oct 18 '16 at 16:55

You can do this trick mentioned here:


but with one thing that isn't mentioned there is you will be able to just do: $_product->getAttributeCode() it will work and give you the value.

As doing this

$product->getCustomAttribute( 'attribute_code' )->getValue() 

will return null;

| 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.