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">
                <argument name="top_links" xsi:type="string">top_links</argument>

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

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

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">
            <argument name="block_id" xsi:type="string">top_links</argument>

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

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

calling in phtml like below instead of yours

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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