我创建了一个左侧边栏导航模板,它可以浏览两个类别(动态)和手动添加的CMS页面(请参阅下面的代码),并且我设法让活动状态正常工作但我理想情况下喜欢动态浏览页面,然后添加活动状态。有任何想法吗?提前谢谢。
<?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 ?>
答案 0 :(得分:0)
Robert Kent的an example of dynamically building Magento CMS page lists通过迭代CMS页面集合并排除非活动和系统页面(无cookie,无路由等)来工作。
要处理有效状态,只需将$current_page
与$page['identifier']
循环中的foreach
值进行比较:
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']
);
}
}