I'm using Magento 2 and I want to add block to all my category pages , by directly writing it into the code in the app/design/frontend/Venustheme/yume/Magento_Catalog/templates/product/list.phtml
page , there is any way to call the block there, and not through the admin panel?
-
The same question I found on stackoverflow. Answered here: stackoverflow.com/questions/36594339/…– Gábor SzabóCommented Nov 24, 2016 at 15:43
2 Answers
In your theme folder, add a Magento_Catalog/layout/catalog_category_view.xml
layout file. Load your block into the parent block with the following:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="category.products.list">
<block name="your.cms.block" as="something" class="Magento\Cms\Block\Block">
<arguments>
<argument name="block_id" xsi:type="string">[put your block ID here]</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
In your list.phtml
simply call <?php echo $block->getChildHtml('something') ?>
. Of course you should name your blocks differently, but I hope you got the general idea.
-
Hi , this works fine , thank you very much ! but when I print my block , it appears inside the <li></li> , here is my code .. what I'm doying wrong ? <?php echo($iterator == count($_productCollection)+1) ? '</li>' : '' ?> <?php $contator++; if($contator==3){ echo $block->getChildHtml('category-middlemsg'); } ?> <?php endforeach; ?>– alexcrCommented May 11, 2016 at 10:06
-
What is it? Anyway, you should totally avoid using
count()
inside aforeach
loop, it is a well-known performance killer. Assign that value to a variable before the loop and compare the variable instead.– fmrngCommented May 11, 2016 at 10:09 -
is the counter I did before the loop , so I can count the products , as I want to print my block after 3 products , now is printing the block inside the <li></li> , I want to print it outside..– alexcrCommented May 11, 2016 at 10:12
-
Edit your question and post your code for us to understand, or open a new question maybe.– fmrngCommented May 11, 2016 at 10:12
-
1Do something like this
<?php if ($iterator === 6) echo '</li><li class="item product product-item">' . $block->getChildHtml('something') ?>
– fmrngCommented May 11, 2016 at 10:47
You probably want to use the layout xml to add a block instead of a template file.
In your module, add a file view/frontend/layout/catalog_category_view.xml.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="whatever.container.i.want.to.reference">
<block class="Namespace\Modulename\Block\BlockClass" name="my.block.name" template="Namespace_Modulename::path/to/template/file.phtml" />
</referenceContainer>
</body>
</page>
Depending on the type of block you are creating, you don't always need a template file.
-
Is just a block with a div as a content , is just that everytime I will need to edit it , I want to edit the block throught the adminpanel - content - blocks - my block , that's why I need a way to call it directly incode , also because I want to put it after 6 products have been listed , so into my list.phtml , I made a counter , and after the first 6 products are listed , I want to add my block , so I need a way to call it directly in code , the code you posted is ok , but I can't just put it after 6 products.. I don't know if I explained myself well..– alexcrCommented May 10, 2016 at 18:04