0

How to give condition in xml file for showing different block in different pages. Like I place a block name "Footer Block" in default xml file, and I want different footer for different pages.

 <referenceContainer name="footer">
 <block class="Magento\Cms\Block\Block" name="footer_links_block">
 <arguments>
 <argument name="block_id" xsi:type="string">footer_links_block</argument>
 </arguments>
 </block>
 </referenceContainer>

I want this that this footer only will show in home page and I want to different footer block for other pages. How I can do it in Magento 2.2.5

Thank You

asked Sep 7, 2018 at 14:37
3
  • Please clarify, by different pages you mean different type of pages or like different category pages/ different product pages? Commented Sep 7, 2018 at 14:58
  • I want to show footer in home page and different footer for other pages. And this footer section I have done by block. In default.xml file I fetch this block. So I want any condition that I can differentiate in xml file. How I can do this. Thank you for your reply. Commented Sep 8, 2018 at 7:04
  • Hello @Vivek Kumar, is there any way to do this functionality? Sorry, that before I didn't clarify the question clearly. Please help, if is there any way to do this. Thank you. Commented Sep 10, 2018 at 7:32

2 Answers 2

1

Okay, I got the solution. I call the .phtml page into .xml file, like this.

default.xml

 <referenceContainer name="footer">
 <container name="footer-custom" htmlTag="div" htmlClass="footer-custom">
 <block class="Magento\Framework\View\Element\Template" name="footer_upper" template="Magento_Theme::html/footer.phtml"> 
 </block> 
 </container>
 </referenceContainer>

and then in .phtml file I place the condition and fetch the block there, like this.

footer.phtml

<?php
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $cmsPage = $objectManager->get('\Magento\Cms\Model\Page');
 $cmsIdent = $cmsPage->getIdentifier();
?>
<?php
 if ($cmsIdent == 'home') {
 echo $block->getLayout()
 ->createBlock('Magento\Cms\Block\Block')
 ->setBlockId('footer_links_block')
 ->toHtml();
 } else {
 echo $block->getLayout()
 ->createBlock('Magento\Cms\Block\Block')
 ->setBlockId('footer_links_block_for_others')
 ->toHtml();
 }
?>

It works perfect for me. Thank you.

answered Sep 10, 2018 at 12:42
0

If you have to show it only for home page, then more appropriate solution will be to show them via cms_index_index.xml. In that way, you can manage everything via XML too including containers if you have to add them in near future.

answered Mar 19, 2019 at 10:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.