1

I've created a left sidebar navigation template that pulls through both categories (dynamically) and CMS pages which have been added manually (see code below) and I've managed to get the active state working but I would ideally like to pull through the pages dynamically and then add the active state. Any ideas? Thanks in advance.

<?php $_menu = $this->renderCategoriesMenuHtml(0,'level-top') ?>
<?php if($_menu): ?>
<?php
$current_page = '';
/*
* Check to see if its a CMS page
* if it is then get the page identifier
*/
if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms'):
    $current_page = Mage::getSingleton('cms/page')->getIdentifier();
endif
?>
<nav class="nav-container">
<ul id="nav">
    <?php echo $_menu; ?>
    <li <?php if ($current_page == 'home') { echo 'class="active"'; } else { echo 'class="home"'; } ?>><a href="<?php echo $this->getUrl('home')?>"><span><?php echo $this->__('Home') ?></span></a></li>
    <li <?php if ($current_page == 'about') { echo 'class="active"'; } else { echo 'class="about"'; } ?>><a href="<?php echo $this->getUrl('about')?>"><span><?php echo $this->__('About') ?></span></a></li>
    <li <?php if ($current_page == 'faqs') { echo 'class="active"'; } else { echo 'class="faqs"'; } ?>><a href="<?php echo $this->getUrl('faqs')?>"><span><?php echo $this->__('FAQS') ?></span></a></li>
    <li <?php if ($current_page == 'contacts') { echo 'class="active"'; } else { echo 'class="contacts"'; } ?>><a href="<?php echo $this->getUrl('contacts')?>"><span><?php echo $this->__('Contact Us') ?></span></a></li>
    <li <?php if ($current_page == 'artworks') { echo 'class="active"'; } else { echo 'class="artworks"'; } ?>><a href="<?php echo $this->getUrl('artworks')?>"><span><?php echo $this->__('Artworks') ?></span></a></li>
    <li <?php if ($current_page == 'how-it-works') { echo 'class="active"'; } else { echo 'class="how-it-works"'; } ?>><a href="<?php echo $this->getUrl('how-it-works')?>"><span><?php echo $this->__('How it Works') ?></span></a></li>
</ul>
</nav>
<?php endif ?>
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Gareth Daine
  • 4,016
  • 5
  • 40
  • 67

1 Answers1

0

Robert Kent has an example of dynamically building Magento CMS page lists that works by iterating over the CMS page collection and excluding inactive and system pages (no-cookies, no-route, etc.).

To handle active states simply compare $current_page against the value of $page['identifier'] inside the foreach loop:

foreach ( $_menu_cms as $cmspage ) {
  $page = $cmspage->getData();
  if ( ! in_array( $page['identifier'], array( 'no-route', 'enable-cookies' ) ) ) {
    $class = ( $page['identifier'] == $current_page ? 'active' : '' );
    printf( '<li><a href="%s" title="%s" class="%s">%s</a></li>',
      $this->getUrl( $page['identifier'], $this->htmlEscape( $page['title'] ), $class, $page['title']
    );
  }
}
Steve Grunwell
  • 896
  • 6
  • 21