get CMS PAGE information by identifier (URL KEY)

Sometimes, you want to use information like title or content from a CMS page (or multiple CMS pages) in another page. For example, you want to show a part of “Welcome” page in homepage. How can you do the task?

Today I will show you that how you can use CMS pages in another page.

    • To use information of a CMS page in another page, you will need the identifier (URL key) of this CMS page. Then in the page you want to use the information, put some codes as following:
$aCmsPage = Mage::getModel('cms/page')->load('URL-key-of-this-page', 'identifier');
$theTitle = $aCmsPage->getTitle();
$theContent = $aCmsPage->getContent();
Then you can use this title and content anywhere you want!
    • To get information from multiple CMS pages, we need to use CMS page collection:
$collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());
$collection->getSelect()->where('is_active = 1'); // use the filter in the case you want to get only enabled CMS pages.
foreach ($collection as $page) {
    $theIdentifier = $page->getIdentifier();
    $theTitle = $page->getTitle();
    $theContent = $page->getContent();
    ....// use these like you want!
}

在这个基础上我加上一个IF来取得想要的PAGE内容 我的代码如下

<?php

$collection = Mage::getModel(‘cms/page’)->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());

$collection->getSelect()->where(‘is_active = 1’); // use the filter in the case you want to get only enabled CMS pages.

foreach ($collection as $page)

{

$theIdentifier = $page->getIdentifier();

if($theIdentifier==”product-care”)

{

$theTitle = $page->getTitle();

$theContent = $page->getContent();

echo $theContent;

}

}

?>

Leave a Comment