Magento – Categories and subcategories in front / PHTML integrate

Picture

There are several reasons, so an integration of categories and subcategories or. the category links on the front page is useful. Be it as a better and faster user friendliness and search engine optimization.

One can of course manually and resolve this piece by piece with a CMS page, which is then integrated into the side (was here shown).
But whoever would have easily and automatically the current categories etc. will have on the page, so that changes take effect immediately and will work different Store Views, can work with this solution, which I http://fishpig.co.uk/display-categories-and-subcategories-in-magento/ have found.

Here is a relatively simple method called, About the only categories as either, About the only categories and subcategories on the current category or all categories and subcategories may be displayed.

The following codes can be entered directly in PHTML pages. I did this, e.g.. the footer.phtml from the directory app / design / frontend /[base or default]/IhrTemplate/template/page/html used. There, after the last entry.

Code to display only the categories over:

<?php
/*
* http://fishpig.co.uk – Magento Tutorials
*
* Display top level categories
*
**/
?>
<?php $_helper = Mage::helper(&#8216;catalog/category&#8217;) ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<Street>
<?php foreach($_categories as $_category): ?>
<li>
<a href=”<?php echo $_helper->productAttribute($_product, $_product->getName(), ‘name’) ?></h1> Dies aber natürlich nur Vorschläge >getCategoryUrl($_category) ?>”>
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</Street>
<?php endif; ?>

Code on all- and display sub-:

<?php
/*
* http://fishpig.co.uk – Magento Tutorials
*
* Display top level categories and subcategories
*
**/
?>
<?php $_helper = Mage::helper(&#8216;catalog/category&#8217;) ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry(&#8216;current_category&#8217;) ?>
<?php if (count($_categories) > 0): ?>
<Street>
<?php foreach($_categories as $_category): ?>
<li>
<a href=”<?php echo $_helper->productAttribute($_product, $_product->getName(), ‘name’) ?></h1> Dies aber natürlich nur Vorschläge >getCategoryUrl($_category) ?>”>
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel(&#8216;catalog/category&#8217;)->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<Street>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href=”<?php echo $_helper->productAttribute($_product, $_product->getName(), ‘name’) ?></h1> Dies aber natürlich nur Vorschläge >getCategoryUrl($_subcategory) ?>”>
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</Street>
<?php endif; ?>
</li>
<?php endforeach; ?>
</Street>
<?php endif; ?>

About the code categories and the subcategories to be displayed on the active category:

<?php
/*
* http://fishpig.co.uk – Magento Tutorials
*
* Display top level categories and
* subcategories of the current category
*
**/
?>
<?php $_helper = Mage::helper(&#8216;catalog/category&#8217;) ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry(&#8216;current_category&#8217;) ?>
<?php if (count($_categories) > 0): ?>
<Street>
<?php foreach($_categories as $_category): ?>
<li>
<a href=”<?php echo $_helper->productAttribute($_product, $_product->getName(), ‘name’) ?></h1> Dies aber natürlich nur Vorschläge >getCategoryUrl($_category) ?>”>
<?php echo $_category->getName() ?>
</a>
<?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
<?php $_category = Mage::getModel(&#8216;catalog/category&#8217;)->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<Street>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href=”<?php echo $_helper->productAttribute($_product, $_product->getName(), ‘name’) ?></h1> Dies aber natürlich nur Vorschläge >getCategoryUrl($_subcategory) ?>”>
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</Street>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</Street>
<?php endif; ?>

(Note: I have the range

<?php
/*
* http://fishpig.co.uk – Magento Tutorials
*
* Display top level categories and
* subcategories of the current category
*
**/
?>

deliberately left in the instructions, to pay tribute to the author thanks. The area can be removed naturally.)

If you want to assign to the area now have a CSS class with an individual design, This can be done in the following code simply:

<div class =”example”>
<?php
/*
* http://fishpig.co.uk – Magento Tutorials
*
* Display top level categories
*
**/
?>
<?php $_helper = Mage::helper(&#8216;catalog/category&#8217;) ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<Street>
<?php foreach($_categories as $_category): ?>
<li>
<a href=”<?php echo $_helper->productAttribute($_product, $_product->getName(), ‘name’) ?></h1> Dies aber natürlich nur Vorschläge >getCategoryUrl($_category) ?>”>
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</Street>
<?php endif; ?>
</div>

Well, of course, must be made ensprechender another entry in the relevant CSS file. In our example, this was the styles.css in the folder skin/frontend/[base or default]/IhrTemplate/css . Here we have then

.example { text-align: justify; }
.Example A { color:#666; text-decoration: none; }

inserted and then we had a personalized section on the Front Page. It can also be used with li and ul, which are mentioned above. I have removed them for my solution.

A further instructions (comfortable with Magento 1.4.1.1) a little “better” should be also the same author here to find.

Used in Magento versions 1.4.1.1 and 1.5.1.0 Comments? Additions? Notes? Gladly!

Published by Covos

Since 2009 I have been working intensively with Magento. I started with the creation and operation of B2C stores. This was extended through my work in the logistics sector. This resulted first specialized B2E systems. Today I work day-a day with exciting B2C, B2B- and B2E projects and reports in this blog about challenges and give insider tips.

5 comments on “Magento – Categories and subcategories in front / PHTML integrate”

  1. Hey, really great guide =) exactly what I'm looking!!
    However, I unfortunately still a problem, where can I not get … .

    I have the code of “About the code categories and the subcategories to be displayed on the active category:” taken. Now, when I click on a subcategory, Then he hides the sub again and I see only the main categories in the navigation :S .
    What should I do, thus the subcategories are not shown, When I am in a sub-category? Is there already a solution or I'm just the problem?

    1. I think, I had the problem. Therefore I've decided to always display all categories. See e.g.. under http://www.bree-shop.com
      According to the code I'm assuming, that he let in "to the code on the respective sub-categories and display the active category About:"Really shows only the subcategories of each category on active. Thus, if a sub-category displayed, He would try to automatically go to "down". But this is not just.

      1. Hey, thanks for the quick feedback!
        Ja, that would be my last resort … Unfortunately, the categories have so many sub-, that in the end it would look somehow really sucks …

        I have again started looking and found another solution, are also displayed in the sub-, if one is located on a.
        I have just installed and it seems to FUNZ.

        Link to code:
        http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/getting_and_using_categories_and_subcategories

        Maybe you still use it someday 😉

Leave a Reply

Your email address will not be published. Required fields are marked *