添加 "active" class 到顶部菜单,magento 1.9

Add "active" class to topmenu, magento 1.9

我需要在顶部菜单中添加活动 class。而且我已经在论坛上看到了很多主题,但是我没有看到像我这样的代码。你能帮帮我吗我试图 "echo $currentUrl" 但这是我看到的“ localhost.com/app/etc/local.xml “

<?php $_menu = $this->getHtml('level-top') ?>

<?php if($_menu): ?>

<?php
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$baseUrl = Mage::getBaseUrl();

$locale = Mage::app()->getLocale()->getLocaleCode();

$_topMenuItems['ru_RU'] = array(
    /*$this->__('Home') => $baseUrl,*/
    $this->__('IIW, Delivery and payment') => $baseUrl . 'dostavka-oplata.html',
    $this->__('IIW, About us') => $baseUrl . 'pro-nas-kategorija.html',
    $this->__('IIW, Partners') => $baseUrl . 'partneram.html',
    $this->__('IIW, Contacts') => $baseUrl . 'contacts',
    $this->__('IIW, It is interesting') => $baseUrl . 'korisna-informacija.html',
    /* $this->__('IIW, Where to buy') => $baseUrl . 'de-prudbatu.html', */
);
?>

<nav id="nav">
    <ol class="nav-primary">
        <?php
        $i = 0;
        $length = count($_topMenuItems[$locale]);
        foreach ($_topMenuItems[$locale] as $label => $_itemUrl) {
            if ($i == 0) {
                echo $_menu;
            }

            $customMenuClass = ' cm-' . ($i + 1);

            if ($i == 0) {
                $customMenuClass .= ' first';
            } elseif ($i == ($length - 1)) {
                $customMenuClass .= ' last';
            }
            if (trim($currentUrl, '/') == trim($_itemUrl, '/')) {
                $customMenuClass .= ' active';
            }
            ?>

            <li class="level0<?php echo $customMenuClass ?>">
                <a href="<?php echo $_itemUrl ?>" class="level0 "><?php echo $label ?></a>
            </li>
            <?php
            $i++;
        }
        ?>
    </ol>
</nav>
<script>
jQuery(document).ready(function($){
var locationUrl = location.href;
$(".yourmenu a.yourclass").each(function() {
  var menuLinkHref = $(this).attr("href");
  if (~locationUrl.indexOf(menuLinkHref)) {
   $(this).addClass("active");
  }
});
});
</script>

JS解决方案如果你需要 .yourmenu - 菜单的 class,您的 link 必须更改 class .actve; .yourclass - class 已用于菜单中的每个 link(例如“.menu_item_link”)。