0

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();?>
1
  • Can you paste the entire contents of default.xml. Is top_links block a child block of catalog.topnav block ?
    – nikin
    Commented Dec 12, 2018 at 15:05

2 Answers 2

1

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() ?>
0

calling in phtml like below instead of yours

<?php echo $this->getChildHtml('top_links') ?>
2
  • Is this code working fine ? Commented Dec 12, 2018 at 14:29
  • Yeah...Why if you are facing any issue???
    – Ajithkumar
    Commented Dec 12, 2018 at 14:52

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.