Magento – All CMS Pages in a list w/ current page highlighted

Ok, so this might be easy for a seasoned Magento Front Ender to work out, but it’s always nice to have this type of code available for people to copy and paste.

Sometimes, you’ll be required to output a list of CMS pages on your store for easier navigation for the user. In this case, I was asked to add a sidebar to all CMS pages containing other active pages.

Firstly, create a page in your theme folder, something like template/page/html/cmsPages.phtml

Pop the following code in the file:

<?php 
	$currIdent = Mage::getSingleton('cms/page')->getIdentifier(); //We need the identifier of the current page for later use
	$currTitle   = Mage::getSingleton('cms/page')->getTitle();
?>

<div class="related-pages">
	<h2><?php echo $currTitle; ?></h2>
		<?php 
		 $pages = Mage::getModel('cms/page')->getCollection()
		        ->addFieldToFilter('is_active',1)
		        ->addFieldToFilter('identifier',array(array('nin'=>array('privacy-policy','terms-conditions')))); //Use this to exclude any pages you don't want in the list
		?>
		<ul>
			<?php
				foreach($pages as $page)
				{
				    //var_dump($page->getData()); //Unquote this if you want to see other attributes.
				    $ident = $page->getIdentifier(); //Identifier of page in loop
				    if ($ident == $currIdent) { //If current page identifier matches looped identifier
				    	$class = 'class="current"'; 
				    }else{
				    	$class = '';
				    }
					echo '<li ' . $class .'><a href="' . $this->getUrl($ident) .'">' . $page->getTitle() . '</a></li>'; 
				}
			?>
		</ul>
</div>

Once that’s created, go into your local.xml and add the following to all CMS pages. This is under the assumption that your CMS pages contain a right or left block:

    <cms_page>
        <reference name="right">
            <block type="page/html" name="cms_list" as="cms_list" template="page/html/cmsPages.phtml" />  
        </reference>
    </cms_page>

And there we go. Simple as that!