Add a CMS Page to the Navigation Menu
Hi,
[Magento version 1.7]
Today we’re gonna create a simple module to allow the addition of a link to a specific cms page to your website navigational menu, maybe an About Page or Contact Us. We can simply do that by editing the top navigation file located at /app/design/frontend/abc/theme/template/catalog/navigation/top.phtml but this would leave a .last class attribute to the last category and this might miss the design a bit. So what we will be doing is overriding renderCategoriesMenuHtml function which is responsible for drawing the menu items.
Module Installation File:
/app/etc/modules/ABC_Catalog.xml
<?xml version="1.0"?> <config> <modules> <ABC_Catalog> <active>true</active> <codePool>local</codePool> </ABC_Catalog> </modules> </config>
Moving on to the module configuration:
Module Configuration File:
/app/code/local/ABC/Catalog/etc/config.xml
<?xml version="1.0"?> <config> <modules> <ABC_Catalog> <version>1.0</version> </ABC_Catalog> </modules> <global> <blocks> <catalog> <rewrite> <navigation>ABC_Catalog_Block_Navigation</navigation> </rewrite> </catalog> </blocks> </global> </config>
Now to some actual code, we are overriding the render category item with a slightly modified version that takes an extra parameter that specifies whether to append the last class attribue or not to the last drawn category.
Navigation Class:
/app/code/local/ABC/Catalog/Block/Navigation.php
<?php
/**
* Catalog navigation
*
* @category Mage
* @package Mage_Catalog
* @author Yehia Abdel Salam <yehiasalam@cairocubicles.com>
*/
class ABC_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation
{
/**
* Render categories menu in HTML
*
* @param int Level number for list item class to start from
* @param string Extra class of outermost list items
* @param string If specified wraps children list in div with this class
* @return string
*/
public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '', $markLast = true)
{
$activeCategories = array();
foreach ($this->getStoreCategories() as $child) {
if ($child->getIsActive()) {
$activeCategories[] = $child;
}
}
$activeCategoriesCount = count($activeCategories);
$hasActiveCategoriesCount = ($activeCategoriesCount > 0);
if (!$hasActiveCategoriesCount) {
return '';
}
$html = '';
$j = 0;
foreach ($activeCategories as $category) {
$html .= $this->_renderCategoryMenuItemHtml(
$category,
$level,
( $markLast ? ($j == $activeCategoriesCount - 1) : false ) ,
($j == 0),
true,
$outermostItemClass,
$childrenWrapClass,
true
);
$j++;
}
return $html;
}
}
Now we could open /app/design/frontend/abc/theme/template/catalog/navigation/top.phtml and add something like this, note that we set the last parameter of renderCategoriesMenuHtml to false so it wont append any last classes
<?php $_menu = $this->renderCategoriesMenuHtml(0,'level-top', '', false) ?>
<?php if($_menu): ?>
<div class="nav-container">
<ul id="nav">
<?php echo $_menu ?>
<li class="level0 nav-6 level-top last parent">
<a href="<?php echo $this->getUrl('about-us')?>" class="level-top">
<span>Programs & Services</span>
</a>
</li>
</ul>
</div>
<?php endif ?>
And thats how you create an entire module to do trivial stuff in magento.