2

How to add active state to CMS Page in Top Menu? I get active class if i'm on category page. I'm add items to Top Navigation used categories rewrites urls in magento.

I need help.

  • can you please give me screenshot? – Amit Bera Feb 9 '15 at 5:04
1

Here is the proper way to add a cms page link to the main menu without using the event page_block_html_topmenu_gethtml_before.
This way you will be able to add an active state to it also.
So create an observer in one of your modules that can look like this:

public function addCmsPage($observer) 
{
    //your page identifier
    $cmsPageIdentifier = 'page_idendifier_here';
    //get the menu instance
    $menu = $observer->getMenu();
    //load the page
    $page = Mage::getModel('cms/page')->load($cmsPageIdentifier, $identifier);
    //if the page exists
    if ($page->getId()) {
        $active = false;
        // Get the full action name for the request.
        $fullActionName = Mage::app()->getFrontController()->getAction()->getFullActionName();
        //check if we are on the view page action for the required page
        if ($fullActionName == 'cms_page_view') {
             $pageId = Mage::app()->getRequest()->getParam('page_id', $this->getRequest()->getParam('id', false)); 
             if ($pageId == $page->getId()) {
                 $active = true;
             }
        }
        //build a new menu item node
        $data = array(
            'name' => $page->getTitle(),
            'id' => 'cms-'.$cmsPageIdentifier,
            'url' => Mage::getUrl('', array('_direct'=>$cmsPageIdentifier)),
            'is_active' => $active
        );
        $pageNode = new Varien_Data_Tree_Node($data, 'id', $tree, $menu);
        //attach the node to the menu
        $menu->addChild($pageNode);
    }
}
0

Add this code after to $menu on topmenu file.

<?php
$urlkey = 'contactus;aboutus;team'; // Define a identifier cms pages using ';'
$arrayUrlKey = explode(";", $urlkey);
foreach($arrayUrlKey as $url){
    $condition = explode(':', $url);
    $page = Mage::getModel('cms/page')->load($condition[0], 'identifier');
    if($condition[1]) $class = 'menu-item-'.$condition[1];
    if($page->getIsActive()) {
?> 
    <li class="level0 level-top parent custom-block <?php if($routeName == 'cms' && $identifier == $page->getIdentifier()) {echo " active";}?> <?php echo $class;?>">
        <a href="<?php echo Mage::helper('cms/page')->getPageUrl($condition[0]);?>" class="level-top">
            <span><?php echo $page->getTitle();?></span>
        </a> 
    </li> 
<?php 
    }
}
?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.