Hide navigation items with CSS
Note: A much simpler solution has been described here: Hide Categories from the Navigation with Pure CSS.
I made a small modification on method drawItem(). The rendered source will give out an individual id for each point in navigation, so this is a small hide-by-css hack.
app/code/core/Mage/Catalog/Block/Navigation.php, around Line 134
Replace
- /**
- * Enter description here...
- *
- * @param Mage_Catalog_Model_Category $category
- * @param int $level
- * @param boolean $last
- * @return string
- */
- public function drawItem($category, $level=0, $last=false)
- {
- $html = '';
- if (!$category->getIsActive()) {
- return $html;
- }
- $children = $category->getChildren();
- $hasChildren = $children && $children->count();
- $html.= '<li';
- if ($hasChildren) {
- $html.= ' onmouseover="toggleMenu(this,1)" onmouseout="toggleMenu(this,0)"';
- }
- $html.= ' class="level'.$level;
- if ($this->isCategoryActive($category)) {
- $html.= ' active';
- }
- if ($last) {
- $html .= ' last';
- }
- if ($hasChildren) {
- $cnt = 0;
- foreach ($children as $child) {
- if ($child->getIsActive()) {
- $cnt++;
- }
- }
- $html .= ' parent';
- }
- $html.= '">'."n";
- $html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$category->getName().'</span></a>'."n";
- //$html.= '<span>'.$level.'</span>';
- if ($hasChildren){
- $j = 0;
- $htmlChildren = '';
- foreach ($children as $child) {
- if ($child->getIsActive()) {
- $htmlChildren.= $this->drawItem($child, $level+1, ++$j >= $cnt);
- }
- }
- if (!empty($htmlChildren)) {
- $html.= '<ul class="level' . $level . '">'."n"
- .$htmlChildren
- .'</ul>';
- }
- }
- $html.= '</li>'."n";
- return $html;
- }
With
- /**
- * Enter description here...
- *
- * @param Mage_Catalog_Model_Category $category
- * @param int $level
- * @param boolean $last
- * @return string
- * modified for css hide by jan212@05.05.2008
- */
- public function drawItem($category, $level=0, $last=false)
- {
- $html = '';
- if (!$category->getIsActive()) {
- return $html;
- }
- $children = $category->getChildren();
- $hasChildren = $children && $children->count();
- $html.= '<li';
- if ($hasChildren) {
- $html.= ' onmouseover="toggleMenu(this,1)" onmouseout="toggleMenu(this,0)"';
- }
- //modified for css hide from jan212
- $cid = $category->getID();
- //
- $html.= ' class="level'.$level;
- //
- if ($this->isCategoryActive($category)) {
- $html.= ' active ';
- }
- if ($last) {
- $html .= ' last';
- }
- if ($hasChildren) {
- $cnt = 0;
- foreach ($children as $child) {
- if ($child->getIsActive()) {
- $cnt++;
- }
- }
- $html .= ' parent';
- }
- //modified for css hide from jan212
- $html.= '" id="nav-'.$level.'-'.$cid.'">'."n";
- //
- $html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."n";
- //$html.= '<span>'.$level.'</span>';
- if ($hasChildren){
- $j = 0;
- $htmlChildren = '';
- foreach ($children as $child) {
- if ($child->getIsActive()) {
- $htmlChildren.= $this->drawItem($child, $level+1, ++$j >= $cnt);
- }
- }
- if (!empty($htmlChildren)) {
- $html.= '<ul class="level' . $level . '">'."n"
- .$htmlChildren
- .'</ul>';
- }
- }
- $html.= '</li>'."n";
- return $html;
- }
No you’ll see e.g. something like
- <li class="level0" id="nav-0-5">
in the rendered source of your page menu, e.g. for category 5 like above. The id is based on the following sheme: nav(prefix)-x(Level)-xxxx(Unique Category ID) and inserted to every <li> point.
To hide the navigation entry (for human eyes only) simply create new entries in menu.css like this
- /* css hide from horizontal navigation */
- #nav-0-5 {display:none;} /*examples hides category 5 @ level 0 */
- #nav-1-6 {display:none;} /*examples hides category 6 @ level 1*/
Remember: Every ID is unique
Regards Jan Fervers
The following was contributed by tilzinger
If you don’t want to change core files do this instead
To enable this feature without changing core code put the following PHP into app/code/local/STORENAME/Catalog/Block/Navigation.php
Remember to replace all instances of STORENAME with your store name. e.g. AcmeCompany
- <?php
- /**
- * Magento
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Open Software License (OSL 3.0)
- * that is bundled with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://opensource.org/licenses/osl-3.0.php
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@magentocommerce.com so we can send you a copy immediately.
- *
- * @category Mage
- * @package Mage_Catalog
- * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
- * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- */
- /**
- * Catalog navigation
- *
- * @category Mage
- * @package Mage_Catalog
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class STORENAME_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation
- {
- public function drawItem($category, $level=0, $last=false)
- {
- $html = '';
- if (!$category->getIsActive()) {
- return $html;
- }
- $children = $category->getChildren();
- $hasChildren = $children && $children->count();
- $html.= '<li';
- /*
- Don't need stupid JS. Take care of it with CSS
- if ($hasChildren) {
- $html.= 'onmouseover="toggleMenu(this,1)" onmouseout="toggleMenu(this,0)"';
- }
- */
- //modified for css hide from jan212
- $cid = $category->getID();
- //
- $html.= ' class="level'.$level;
- //
- if ($this->isCategoryActive($category)) {
- $html.= ' active ';
- }
- if ($last) {
- $html .= ' last';
- }
- if ($hasChildren) {
- $cnt = 0;
- foreach ($children as $child) {
- if ($child->getIsActive()) {
- $cnt++;
- }
- }
- $html .= ' parent';
- }
- //modified for css hide from jan212
- $html.= '" id="nav-'.$level.'-'.$cid.'">'."n";
- //
- $html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."n";
- //$html.= '<span>'.$level.'</span>';
- if ($hasChildren){
- $j = 0;
- $htmlChildren = '';
- foreach ($children as $child) {
- if ($child->getIsActive()) {
- $htmlChildren.= $this->drawItem($child, $level+1, ++$j >= $cnt);
- }
- }
- if (!empty($htmlChildren)) {
- $html.= '<ul class="level' . $level . '">'."n"
- .$htmlChildren
- .'</ul>';
- }
- }
- $html.= '</li>'."n";
- return $html;
- }
Then add this to your app/etc/local.xml file
- <global>
- ... some other code previous to this...
- <blocks>
- <catalog>
- <rewrite>
- <navigation>STORENAME_Catalog_Block_Navigation</navigation>
- </rewrite>
- </catalog>
- </blocks>
- </global>


