0

I want to get CMS page content and title in phtml template file. How can I get this?

I am trying below code to get content but it's not working -

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$content = $objectManager->create('\Magento\Cms\Model\Page');
$content->load('my_page_identifier', 'identifier');
echo $content->getContent();

Please provide a solution.

2 Answers 2

2

It's wysiwyg editor content. So, you need to use \Magento\Cms\Model\Template\FilterProvider class to filter cms page content and display.

Follow this below code :

Using construct method

protected $_filterProvider;

public function __construct(
    .....
    \Magento\Cms\Model\Template\FilterProvider $filterProvider
    .....
) {
    .....
    $this->_filterProvider = $filterProvider;
    .....
}

public function getContent($identifier)
{
    return $this->_filterProvider->getPageFilter()->filter($this->getPage($identifier)->getContent());
}

Using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$content = $objectManager->create('\Magento\Cms\Model\Page');
$myCmsData = $content->load('my_page_identifier', 'identifier');
$cmsContent = $objectManager->get('\Magento\Cms\Model\Template\FilterProvider')->getPageFilter()->filter($myCmsData->getContent());
echo $cmsContent;
7
  • I want to get the content directly in template file using objectManager, don't want to create a custom module.. In above code I don't see a code which loads the cms page using identifier.
    – chanchal
    Commented Aug 29, 2019 at 5:36
  • Use my object manager method code in your phtml file. Commented Aug 29, 2019 at 5:37
  • Just check my updated answer of object manager method and use in your phtml. Let me know if still not working for you. Commented Aug 29, 2019 at 5:39
  • Yes I got the point but I am asking that the how the object manager will load the specific cms page. I don't see any code which is loading through identifier.
    – chanchal
    Commented Aug 29, 2019 at 5:39
  • Please check my updated answer. Commented Aug 29, 2019 at 5:39
0

Try with below code, Hope its work for you...

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$content = $objectManager->create('\Magento\Cms\Model\PageFactory');
$content->load('my_page_identifier', 'identifier');
echo $content->getContent();
2
  • After, $objectManager->create() you don't need to again create object. Please remove last create() method. Commented Aug 29, 2019 at 5:36
  • 1
    yes, writing by mistake. :) Commented Aug 29, 2019 at 5:41

Your Answer

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

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