0

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.

2
  • Which php code is not working and on which file? Jun 14, 2018 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, 2018 at 11:26

5 Answers 5

1

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.

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

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"}}
0

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"}}
1
  • ohh you can also check the accepted answer above... its also correct
    – 1990rk4
    Jun 14, 2018 at 12:33
0

Please try the below instead of your code:

<div title="<?= $_product->getAttributeText("manufacturer"); ?>"></div>
6
  • 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, 2018 at 11:31
  • Check the updated answer please. title is not for div. Jun 14, 2018 at 11:35
  • i want to get the title for the div,
    – Compu Scie
    Jun 14, 2018 at 11:42
  • Please update the js code on your question. Jun 14, 2018 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. Jun 14, 2018 at 11:46
0

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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.