Is there any way to add a simple php code:

<div title="<?php echo $_product->getAttributeText("manufacturer"); ?>"></div>

to a block we create under content page. It can't execute PHP code as far as I'm concerned.

  • Which php code is not working and on which file? – Sukumar Gorai Jun 14 '18 at 11:24
  • the one above is giving me errors of unexpected token <, but when i directly write it on phtml file it works fine – Compu Scie Jun 14 '18 at 11:26

I assume that you want to call PHP code in a static block.

You can do so by calling a phtml file in the static block and write your PHP code in the phtml file.

To call phtml file in static block, you need to write code like this:

{{block class="Path\To\Block\Class" template="Namespace_Module::filename.phtml"}}

Since you are trying to get product manufacturer, I think you need to use block class name as Magento\Catalog\Block\Product.

  • How may i get the data from this block to my static block – Compu Scie Jun 14 '18 at 12:08
  • @CompuScie, sorry I could understand your comment? – Mohit Kumar Arora Jun 14 '18 at 12:46

You cannot add PHP code into static blocks

but you can add a link to your phtml file into static block

  • Create your sample.phtml file
  • write your code in sample.phtml

Then add this file into the static file as

{{block type="core/template" template="catalog/category/sample.phtml"}}

The following syntax will help you:

Basically, you need to call .phtml file to execute your php file or php code;

{{block class="path-to-block-class" template="module-name::your-filename.phtml"}}
  • ohh you can also check the accepted answer above... its also correct – 1990rk4 Jun 14 '18 at 12:33

Please try the below instead of your code:

<div title="<?= $_product->getAttributeText("manufacturer"); ?>"></div>
  • Uncaught TypeError: Cannot read property 'title' of null, when i try to get the value by JS, also now it just puts php code to the browser – Compu Scie Jun 14 '18 at 11:31
  • Check the updated answer please. title is not for div. – Sukumar Gorai Jun 14 '18 at 11:35
  • i want to get the title for the div, – Compu Scie Jun 14 '18 at 11:42
  • Please update the js code on your question. – Sukumar Gorai Jun 14 '18 at 11:45
  • 1
    That code will only work if it is added to a phtml file. Verify that the file you are adding that code snippet to actually has a phtml extension. – ProcessEight Jun 14 '18 at 11:46

Your above code has small mistake, which is you are including double quote inside another double quote. Replace your code with the below code:

<div title="<?php echo $_product->getAttributeText('manufacturer'); ?>"></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.