1

How to show CMS pages in following format on catalog result pages.

On Catalog search result pages, i want to show second section in the form of CMS pages after product list or grid based on searched parameter query string. I tried below codes.

<body>
    <referenceContainer name="content">
        <block class="CMS/Search/Block/Search" name="cmssearch.table" template="CMS_Search::search-cms.phtml"/>
    </referenceContainer>
</body>

<?php
namespace CMS\Search\Block;
use Magento\Framework\View\Element\Template;

 protected  _resource;

class Search extends Template
{
public function __construct(Context $context,
\Magento\Framework\App\ResourceConnection $resource)
  {
    $this->_resource = $resource;
    parent::__construct($context);

  }

    public function getResultsofCMSPages()
    {
        $query = 'asdf';
        $connection = $this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
        $tbl = $connection->getTableName('cms_page');
        $result = $connection->fetchAll('SELECT title,content,url FROM `'.$tbl.'` WHERE `is_active`=1' and `content` LIKE '%'.$query.'%' );
        return $result;
    }
}

Using couple of ref. as follows: Magento 2 : How to Fetch CMS Page Content Using Search

At catalog search page, it should show as follows:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.