2

I have two CMS pages in magento 2 . I want to call one cms page to another cms page.

anyone can help me.

2
  • I don't think it is possible, for these types of task we use static block and call static block in other cms pages Mar 6 '19 at 10:30
  • actually i have used layout xml in another cms. so i can not use block instead of pages Mar 6 '19 at 10:42
0

Add following xml in Design -> Layout Update XML

<referenceContainer name="content">
    <block class="SR\MagentoCommunity\Block\Page" name="another_cms_page" after="cms_page">
        <arguments>
            <argument name="page_id" xsi:type="number">7</argument>
        </arguments>
    </block>
</referenceContainer>

enter image description here

Now create app/code/SR/MagentoCommunity/Block/Page.php

<?php
namespace SR\MagentoCommunity\Block;

use Magento\Cms\Api\PageRepositoryInterface;

class Page extends \Magento\Framework\View\Element\AbstractBlock implements
    \Magento\Framework\DataObject\IdentityInterface
{
    /**
     * @var \Magento\Cms\Model\Template\FilterProvider
     */
    private $filterProvider;

    /**
     * @var PageRepositoryInterface
     */
    private $pageRepository;

    /**
     * Page constructor.
     *
     * @param \Magento\Framework\View\Element\Context $context
     * @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
     * @param PageRepositoryInterface $pageRepository
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Context $context,
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        PageRepositoryInterface $pageRepository,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->filterProvider = $filterProvider;
        $this->pageRepository = $pageRepository;
    }

    /**
     * Prepare global layout
     *
     * @return $this
     */
    protected function _prepareLayout()
    {
        $pageId = $this->getPageId();
        $cmsPage = $this->pageRepository->getById($pageId);
        $this->setPage($cmsPage);
        return parent::_prepareLayout();
    }

    /**
     * Prepare HTML content
     *
     * @return string
     */
    protected function _toHtml()
    {
        $html = $this->filterProvider->getPageFilter()->filter($this->getPage()->getContent());
        return $html;
    }

    /**
     * Return identifiers for produced content
     *
     * @return array
     */
    public function getIdentities()
    {
        return [\Magento\Cms\Model\Page::CACHE_TAG . '_' . $this->getPage()->getId()];
    }
}

[Update]

Add following code inside cms block content:

{{block class="SR\MagentoCommunity\Block\Page" page_id="7"}}
4
  • I have banner and some content in that page i want to call another cms page after banner and content Mar 7 '19 at 4:52
  • check updated answer
    – Sohel Rana
    Mar 7 '19 at 5:37
  • my both pages are cms page, so what should i write instead of "SR\MagentoCommunity\Block\Page" Mar 7 '19 at 6:43
  • its not working for me bro Mar 7 '19 at 11:12

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.