Question

I want to call a static navigation block in topmenu.phtml

I defined that in default.xml

<block class="Magento\Cms\Block\Block" name="top_links">
            <arguments>
                <argument name="top_links" xsi:type="string">top_links</argument>
            </arguments>
</block>

And calling in .phtml like this.

<nav class="navigation" data-action="navigation">
    <ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
       <?php echo $this->getLayout()->getBlock('top_links')->toHtml();?>
    </ul>
</nav>

But its not working please suggest how to do this.

I don't want to call direct on .phtml like this.

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
Was it helpful?

Solution

Change these lines in the default.xml file

From :

<block class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav" template="Magento_Theme::html/topmenu.phtml" ttl="3600" before="-"/>

To :

   <referenceBlock name="catalog.topnav">
        <block class="Magento\Cms\Block\Block" name="top_links">
            <arguments>
            <argument name="block_id" xsi:type="string">top_links</argument>
            </arguments>
        </block>
    </referenceBlock>

Now, in topmenu.phtml file, just include the below code at desired line.

<?= $block->getChildHtml() ?>

OTHER TIPS

calling in phtml like below instead of yours

<?php echo $this->getChildHtml('top_links') ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top