【问题标题】:Retrieve CMS Pages Dynamically and Add Active State Based on Current Page Identifier in Magento动态检索 CMS 页面并根据 Magento 中的当前页面标识符添加活动状态
【发布时间】:2012-04-03 17:31:07
【问题描述】:

我已经创建了一个左侧边栏导航模板,它可以通过手动添加的类别(动态)和 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 ?>

【问题讨论】:

    标签: magento dynamic content-management-system


    【解决方案1】:

    Robert Kent 有 an example of dynamically building Magento CMS page lists,它通过迭代 CMS 页面集合并排除非活动页面和系统页面(无 cookie、无路由等)来工作。

    要处理活动状态,只需将$current_pageforeach 循环内的$page['identifier'] 值进行比较:

    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']
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2012-04-22
      • 1970-01-01
      相关资源
      最近更新 更多