2
<?php
$objectManagerCms = \Magento\Framework\App\ObjectManager::getInstance();
$cmsPage = $objectManagerCms->get('\Magento\Cms\Model\Page');
if($cmsPage->getIdentifier() == "home"){ ?>
<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('home_remains_content')->toHtml(); ?>
<?php  } ?>

I am using the above code on the footer. and the added block is showing on all pages instead of the home page.

Meant on the list and detail page $cmsPage->getIdentifier() is showing Identifier "home" .if after cache refreshed first we are review home page.

4

The problem appears when the cache is on because the footer is cached the same for all pages.
You either move it outside the footer block, but this might not be easy if you need it in a specific case.
Or you can create different cache for the homepage.

For this you can create an after plugin on the Magento\Theme\Block\Html\Footer::getCacheKeyInfo method.
Here is how you can create a plugin: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html

and your plugin class can look like this:

namespace [Vendor]\[Module]\Plugin\Html;

class FooterPlugin
{
    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    private $request;

    public function __construct(\Magento\Framework\App\RequestInterface $request)
    {
        $this->request = $request;
    }

    public function afterGetCacheKeyInfo(
        \Magento\Theme\Block\Html\Footer $subject,
        array $result
    ) {
        $handle = $this->request->getFullActionName();
        $homeHandle = 'cms_index_index';
        $result[] = ($handle === $homeHandle);
        return $result;
    }
}

CLear the cache and you should now have 2 cache versions for the footer. One for homepage and one for the rest of the pages.

Bonus: Don't use Object manager in templates. Use the approach Amit Bera showed to get your content into the page.

(i didn't test the code so watch out for typos).

1
  • Good idea marius.I will use this logic in future – Amit Bera Jun 17 '20 at 8:54
0

If you using object manage in this way then you always get the issue at cache.

Best way to add cms block using the layout.

Create cms_index_index.xml at your theme if it not exits

Than add below code at that layout file

 <?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceBlock name="{NameInLayout}">
            <block class="Magento\Cms\Block\Block" name="home.remains.content">
                <arguments>
                    <argument name="block_id" xsi:type="string">home_remains_content</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

After on phtml call where if($cmsPage->getIdentifier() == "home"){ ?> write the code. At there add below code

<?= $block->getChildHtml('home.remains.content');?>

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.