My name is Clayton McIlrath and I am an entrepreneur currently living in CO. I personally enjoy the process of learning, exploring, and doing all things creative as well as sharing my experiences with others. Being an entrepreneur and business owner, I hope that my experiences may help someone else start their own venture and find success and freedom as I have! Feel free to contact me anytime for questions or opportunities.

close
more

»

«


Magento: Display Categories in Sidebar

It seems my posts lately are always in relation to solving a problem or answering a question for someone else. If you’re looking for a way to display categories in the sidebar, change category display, or create a category menu then this post is probably for you.

Creating the Block

The first thing you need to do is create a block in your layout. Navigate to /app/design/frontend/default/default/layout/catalog.xml

The first thing in your layout is a definition of the default layout noted by the comment “Default layout, loads most of the pages” depending on where you want to put your category nav (right sidebar, left sidebar, footer, etc) you’ll need to define the block a little differently. I’m going to do it with the left sidebar so you see what’s going on.

<reference name="left">
            <block type="catalog/navigation" name="catalog.leftnav" template="catalog/navigation/left_nav.phtml" />
</reference>

Okay, so what i did was locate the left sidebar which is referenced simply as “left” inside those tags i define my block type and template. The next thing you’ll want to do is create that template file, note that i defined it within /app/design/frontend/default/default/template/catalog/navigation/left_nav.phtml so i will need to create that file.

Creating the Template

Once I’ve created the file, it’s time to put in my code to populate my links automatically of all my categories:

<h2>Browse</h2>
<div class="block">
<ul id="nav_category" class="nav_category">
	<?php foreach ($this->getStoreCategories() as $_category): ?>
		<?php echo $this->drawItem($_category) ?>
	<?php endforeach ?>
</ul>
</div>
<?php echo $this->getChildHtml('topLeftLinks') ?>

If you want to take this a step further, you can target subcategories based on current page with this little script (via Pratthost

<?php
$obj = new Mage_Catalog_Block_Navigation();
$store_cats	= $obj->getStoreCategories();
$current_cat 	= $obj->getCurrentCategory();
$current_cat	= (is_object($current_cat) ? $current_cat->getName() : '');
 
foreach ($store_cats as $cat) {
	if ($cat->getName() == $current_cat) {
		echo '<li class="current"><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a>\n<ul>\n";
		foreach ($obj->getCurrentChildCategories() as $subcat) {
			echo '<li><a href="'.$this->getCategoryUrl($subcat).'">'.$subcat->getName()."</a></li>\n";
		}
		echo "</ul>\n</li>\n";
	} else {
		echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a></li>\n";
	}
}
?>

Now refresh the cache and you should be set!

  • http://www.eliezerisrael.com Elie

    Was working through this, and stumbled across an easier way.
    There’s already a template that does the work – catalog/navigation/left.phtml.

    Enabling that in the layout shows the subcategories of the current category.

  • http://www.eliezerisrael.com Elie

    Was working through this, and stumbled across an easier way.
    There’s already a template that does the work – catalog/navigation/left.phtml.

    Enabling that in the layout shows the subcategories of the current category.

  • http://www.vaseem.tk vaseem

    i have done the same but my catalog.xml file dont showing the content i manually added in there
    can u help me how to show categories in left sidebar as i have added new block from cms and the contents are showing in front
    so can i write soem code in cms in that static block that will show categories in left.
    thanks

  • http://www.vaseem.tk vaseem

    i have done the same but my catalog.xml file dont showing the content i manually added in there
    can u help me how to show categories in left sidebar as i have added new block from cms and the contents are showing in front
    so can i write soem code in cms in that static block that will show categories in left.
    thanks

  • Bobby

    Thank you! I needed to override Magento from displaying the categories in the side bar only when at the top level, but disappearing in a filter-like manner once you were in one category. We didn’t want customers to have to hit back to switch categories. This was very helpful! Thanks.

  • Bobby

    Thank you! I needed to override Magento from displaying the categories in the side bar only when at the top level, but disappearing in a filter-like manner once you were in one category. We didn’t want customers to have to hit back to switch categories. This was very helpful! Thanks.

  • http://asbin.gamplong.com arjint

    saya sudah melakukan instruksi di atas. template tampil di depan, tetapi category tidak tampil . apakah ada yang salah?

  • http://asbin.gamplong.com arjint

    saya sudah melakukan instruksi di atas. template tampil di depan, tetapi category tidak tampil . apakah ada yang salah?

  • http://asbin.gamplong.com arjint

    I’ve done the above instructions. template to appear in front, but the category did not appear. whether there is something wrong?

  • http://asbin.gamplong.com arjint

    I’ve done the above instructions. template to appear in front, but the category did not appear. whether there is something wrong?

  • http://seologistics.com pawan sharma

    Thanks for help, It’s working fine

  • http://seologistics.com pawan sharma

    Thanks for help, It’s working fine

  • Qumber Hassan

    Thanks for help, It’s working fine, but categories URLs don’t work properly.
    please solved them out.

  • Qumber Hassan

    Thanks for help, It’s working fine, but categories URLs don’t work properly.
    please solved them out.

  • http://www.ongeremd.com paul

    this is not for adding a navigation menu on the left .
    This tutorial adds a second layered navigation block to the shop.

    It is exactly the same as already displayed on my page .
    Rather useless

  • http://www.ongeremd.com paul

    this is not for adding a navigation menu on the left .
    This tutorial adds a second layered navigation block to the shop.

    It is exactly the same as already displayed on my page .
    Rather useless

  • http://thinkclay.com Clay McIlrath

    I never said anything about second navigation, I claimed it added categories to the sidebar, which prior to 1.4 was a fairly tedious.

  • http://thinkclay.com Clay McIlrath

    I never said anything about second navigation, I claimed it added categories to the sidebar, which prior to 1.4 was a fairly tedious.

  • Amanda

    I am also having problems getting the URLs, my href is blank: href=”"

  • Amanda

    I am also having problems getting the URLs, my href is blank: href=”"

  • Evan S

    For those have trouble with getting links to show up properly:

    Instead of:
    [code]
    echo 'getCategoryUrl($cat).'">'.$cat->getName()."n";
    [/code]

    Use:
    [code]
    echo 'getCategoryUrl($cat).'">'.$cat->getName()."n";
    [/code]

  • Evan S

    For those have trouble with getting links to show up properly:

    Instead of:
    [code]
    echo 'getCategoryUrl($cat).'">'.$cat->getName()."\n";
    [/code]

    Use:
    [code]
    echo 'getCategoryUrl($cat).'">'.$cat->getName()."\n";
    [/code]

  • Evan S

    Ok, well that didn’t display properly at all. Anyway, if you’re having trouble getting your links to work properly using this code, change $this-> in the link creation section to $obj->

  • Evan S

    Ok, well that didn’t display properly at all. Anyway, if you’re having trouble getting your links to work properly using this code, change $this-> in the link creation section to $obj->

  • Azmeehan

    Hi Im working with 1.4 and implemented the above really easily thanks for posting it. However I am receiving the following JS errors every time I hover over a menu item, wondering if anyone has experienced this before and if so what a possible solution may be.

    ReferenceError: Can’t find variable: toggleMenu

  • Azmeehan

    solved it by grabbing fresh JS folder from the magento install zip.

  • Mohamed Musammil

    Hi
    I want to display all categories/Sub categories in all pages ( including product listing and product details page).
    By default, categories list is not displaying in product list,details page.
    How Can I achieve this ?

  • Test

    ggggggggggg

  • Sonia

    Your code works good showing all categories properly. But its showing only in the left side bar of home page. How can this be shown on all pages?

  • Testmail980

    Is it possible to show product listing to the left sidebar?

  • Louis W

    If I wanted to always show the sub categories, how would I query sub categories while inside of the first foreach

  • http://www.justwebdevelopment.com/ Merk

    God, I feel like I should be taken notes! Great work
    use full code thanks

  • http://carbonlenses.com/ flash Website design

    cool man

  • Neeraj Sharma

    nice work

close