Magento – How to give condition in xml file for showing different block in different pages

footermagento-2.2.5xml

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

Best Answer

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.