3

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?

1

2 Answers 2

5

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.

9
  • 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; ?>
    – alexcr
    Commented May 11, 2016 at 10:06
  • What is it? Anyway, you should totally avoid using count() inside a foreach loop, it is a well-known performance killer. Assign that value to a variable before the loop and compare the variable instead.
    – fmrng
    Commented 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..
    – alexcr
    Commented May 11, 2016 at 10:12
  • Edit your question and post your code for us to understand, or open a new question maybe.
    – fmrng
    Commented May 11, 2016 at 10:12
  • 1
    Do something like this <?php if ($iterator === 6) echo '</li><li class="item product product-item">' . $block->getChildHtml('something') ?>
    – fmrng
    Commented May 11, 2016 at 10:47
1

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.

1
  • 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..
    – alexcr
    Commented May 10, 2016 at 18:04

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.