我需要获取phtml模板文件中的cms页面列表。
以下代码将用于magento 1.9x版本
$results = Mage::getModel('cms/page')->getCollection()
->addFieldToFilter(array(
array('field'=> 'title','like' => "%{$search}%"),
array('field'=> 'content','like' => "%{$search}%"),
))
->addFieldToFilter('is_searchable', 1);
如何在magento2.1中获取cms页面
需要在cms页面中添加自定义字段
答案 0 :(得分:0)
我希望这会对你有所帮助,
namespace Creare\DynamicSitemap\Block;
class Dynamicsitemap extends \Magento\Framework\View\Element\Template
{
protected $_storeManager;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Cms\Model\PageFactory $pageFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
array $data = []
)
{
$this->pageFactory = $pageFactory;
$this->_storeManager = $storeManager;
parent::__construct($context, $data);
}
public function getCreareCMSPages(){
$this->getStoreId(); // current store id
$page = $this->pageFactory->create();
foreach($page->getCollection() as $item)
{
echo "page id : ".$item->getId() . ':: page name title : ' . $item->getTitle().'<br/>';
}
die();
$page = $this->pageFactory->create()->load(1);
var_dump($page->getData());
}
}
答案 1 :(得分:0)
将此添加到您的类构造函数
protected $pageCollectionFactory;
public function __construct(
....
\Magento\Cms\Model\ResourceModel\Page\CollectionFactory $pageCollectionFactory,
...
) {
....
$this->pageCollectionFactory = $pageCollectionFactory;
....
}
然后你可以在你的一个类方法中使用它。
$collection = $this->pageCollectionFactory->create();
$collection->addFieldToFilter([
['field'=> 'title','like' => "%{$search}%"],
['field'=> 'content','like' => "%{$search}%",
])
->addFieldToFilter('is_searchable', 1);