如何在magento中创建具有活动和非活动类的自定义菜单?

时间:2016-06-09 05:38:22

标签: magento

我需要magento中的自定义类别菜单。我想在活动类别上有一个活跃的类。当我点击类别时,该类别应该添加活动类。

3 个答案:

答案 0 :(得分:1)

(i)首先从管理面板中创建自定义类别 (ii)然后在代码

中的getCategories()函数中传递类别id

试试这个代码,它对我有用。我希望这对你有用。

<?php
$children = Mage::getModel('catalog/category')->getCategories(2);
?>
<div class="menusecond">
<ul id="second menu">
<?php 
foreach ($children as $category) { ?>
    <li class="mymenu">
	
	<?php $catId = $category->getId(); ?>
	<?php $cat = Mage::getModel('catalog/category')->load($catId); ?> 
	<a href="<?php echo $cat->getUrl(); ?>">
	<?php echo $cat->getName(); ?>
	</a>
	</li>
	
	
<?php
     
}

?>
</ul>
</div>

答案 1 :(得分:1)

我找到了解决方案。我们可以按类别 getUrlKey 识别当前类别。

<?php
$_helper = Mage::helper('catalog/category');
$_categories = Mage::getResourceModel('catalog/category_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('is_active', 1) //only active categories
    ->addAttributeToFilter('include_in_menu', 1);
$currentCategory = Mage::registry('current_category') ;
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$rootCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
$childIds = explode(',',$rootCategory->getChildren());  ?>


  <div class="row">
  <?php if (count($childIds) > 0): ?>
    <ul> 
       <?php foreach($childIds as $_category):
            $_category = Mage::getModel('catalog/category')->load($_category);  ?>
            <?php if ($_category->getIncludeInMenu()) { ?>
            <?php $allsubchild = explode(',',$_category->getChildren());?>

          <?php  foreach($allsubchild as $_sub): ?>
           <?php endforeach; ?>
           <li class="<?php if (strpos($_SERVER['REQUEST_URI'], $_category->getUrlKey()) !== false){ echo "active";} ?>">
           <a href="<?php echo $_category->getUrl(); ?>" title="<?php echo $_category->getName() ?>"><?php echo $_category->getName() ?></a> 
               <?php  }  ?> 
      <ul>
            <?php $ss = 0; foreach($allsubchild as $_sub):
                $_subcategory = Mage::getModel('catalog/category')->load($_sub); 
                ?>
                <?php if ($_subcategory->getIncludeInMenu()) { ?>  
                <?php 
                $uri = $_category->getUrlKey();
                $uri .= "/"; 
                $uri .= $_subcategory->getUrlKey(); // echo $uri; ?>

                <li class="<?php if($_subcategory->getChildrenCount()) {  echo "has-dropdown1"; }?> <?php  if (strpos($_SERVER['REQUEST_URI'], $uri) !== false){ echo "active";} ?>"> 
                    <a href="<?php echo $_subcategory->getUrl(); ?>" title="<?php  echo $_subcategory->getName();  ?>"><?php  echo $_subcategory->getName();  ?></a>
                    <?php $suren = explode(',',$_subcategory->getChildren());?>

                    <ul> 
                    <?php   
                    foreach($suren as $suren1){
                    $suren11 = Mage::getModel('catalog/category')->load($suren1);
                     $_suren1id = $suren11->getId();
                      $internet_count = Mage::getModel('catalog/category')->load($_suren1id)->getProductCount();  
                      if ($internet_count > 0) {  
                     if ($suren11->getIncludeInMenu()) { ?>  
                     <?php 
                $uri1 = $uri; 
                $uri1 .= "/"; 
                $uri1 .= $suren11->getUrlKey();  // echo $uri1; ?>  

                        <li> 
                            <a class="<?php if (strpos($_SERVER['REQUEST_URI'], $uri1) !== false){ echo "active";} ?>" href="<?php echo $suren11->getUrl(); ?>" title="<?php  echo $suren11->getName();  ?>"><?php  echo $suren11->getName();  ?></a> 
                        </li>

                    <?php } ?>
                    <?php } ?>

                    <?php } ?>  
                    </ul>
                </li>
                <?php }?>

           <?php $ss++; endforeach; ?> 
           </ul>
      </li> 
      <?php endforeach; ?>
    </ul>
        <?php endif; ?>
  </div>

答案 2 :(得分:1)

转到app / design / frontend / rwd / default / template / page / html并打开topmenu.phtml文件。找到此代码<?php if($_menu): ?> <nav id="nav"> <ol class="nav-primary"> <?php echo $_menu ?> </ol> </nav> <?php endif ?>

如果您不使用任何类别隐藏此代码,则此代码会以顶级菜单的形式输出您的类别,或者在此代码之前或之后粘贴此代码。

<ul> <li><a href="<?php echo Mage::getBaseUrl(); ?>">HOME</a></li> <li><a href="<?php echo Mage::getBaseUrl(); ?>aboutus">ABOUT US</a></li> <li><a href="<?php echo Mage::getBaseUrl(); ?>your_page_uri">your_page_name</a></li> <li><a href="<?php echo Mage::getBaseUrl(); ?>your_page_uri">your_page_name</a></li> </ul>