2

I am new to Magento 2 and still in the learning process. Being a small business owner, Googling can only get me so far sometimes. I need to add a Hero Image at the top of the product page. If this was Magento 1, I would create an attribute as a text area and then add it to the view.phtml file. The text area is where the caption info would be and everything else would be done via CSS.

File /theme/theme/template/catalog/product/view.phtml

Call the attribute

<?php
    $_product = $this->getProduct();
    $attribute = $_product->getResource()->getAttribute('name');
    if ( is_object($attribute) ) {
        $identifier = $_product->getData("name");
    }
?>

Place this in the view.phtml where I want it to go on the product page itself.

<div class="hero"><?php echo $_product->getData('name'); ?></div>

This obviously does not work in Magento 2.1.8. How would I go about them same thing for Magento 2?

Thank you for any help or pointing me in the right direction.

2
  • It depends on your theme structure. But the main principle is to add attribute to product and then output image url to wanted template. Template of product page is in app/design/frontend/Vendor/Theme/Magento_Catalog. – Kyrylo Romantsov Sep 11 '17 at 7:05
  • I added the same code above into the view.phtml in Magento 2 structure, and I ended up getting a white screen which means it would be a PHP issue. I just assumed I did it wrong or Magento 2 is different and the code I used was incorrect. – Sean Scott Sep 11 '17 at 16:51
4

You can use catalog_product_view.xml to include custom phtml file. Like below code:

<body>
    <referenceContainer name="product.info.main">
        <block class="Magento\Catalog\Block\Product\View"
            name="product.view.extra"
            template="Namespace_Module::custom.phtml" before="-">

    </referenceContainer>
</body>

then in custom.phtml file you can get product attribute like below:

$_product = $block->getProduct();
<div class="hero"><?php echo $_product->getData('name'); ?></div>

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.