Reputation: 33
How do I show on a category page in Magento only the parent category and subcategories?
When a subcategory is clicked the navigation has to stay the same.
Example: If I have
Category 1
After clicking on any subcategory (say subcat 1), I want the same i.e :
Category 1
What I got now:
<?php
if (Mage::registry('current_category'))
{
echo Mage::registry('current_category')->getName();
}
?>
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
// current category is a toplevel category
$loadCategory = $currentCat;
}
else
{
// current category is a sub-(or subsub-, etc...)category of a toplevel category
// load the parent category of the current category
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
// @TODO enhance for more nested category levels to display sub-categories
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a> ';
}
}
?>
But of course this isn't working correct but I can't find a good solution so that Category 1 stays the same when I press subcat 1.
And it would be wonderful if the selected subcategory would be bold.
Upvotes: 2
Views: 1964
Reputation: 613
In your code, in place of
<?php
if (Mage::registry('current_category'))
{
echo Mage::registry('current_category')->getName();
}
?>
Try using
<?php
function getParentTopCategory($c)
{
if($c->getLevel() == 2){
return $c;
} else {
$parentCategory = Mage::getModel('catalog/category')->load($c->getParentId());
return getParentTopCategory($parentCategory);
}
}
if (Mage::registry('current_category'))
{
$category = Mage::registry('current_category');
$t = getParentTopCategory($category);
echo $t->getName();
}
?>
It should work.
EDIT: Here is your complete solution :-
$crcat = Mage::registry('current_category')->getName(); //The current category name is stored in $crcat
function getParentTopCategory($c)
{
if($c->getLevel() == 2){
return $c;
} else {
$parentCategory = Mage::getModel('catalog/category')->load($c->getParentId());
return getParentTopCategory($parentCategory);
}
}
if (Mage::registry('current_category'))
{
$category = Mage::registry('current_category');
$t = getParentTopCategory($category);
if($crcat == $t->getName()) //Check if current category is the topmost category
echo "<b>".$t->getName()."</b>"; //If yes display it as bold (Currently Selected)
else //
echo $t->getName(); //Otherwise display it as normal
echo "<br>";
}
?>
<?php
$currentCat = Mage::registry('current_category');
if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
// current category is a toplevel category
$loadCategory = $currentCat;
}
else
{
// current category is a sub-(or subsub-, etc...)category of a toplevel category
// load the parent category of the current category
$loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
// @TODO enhance for more nested category levels to display sub-categories
}
$subCategories = explode(',', $loadCategory->getChildren());
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($crcat == $cat->getName()) //Check if current category is this subcategory
echo '<b><a href="'.$cat->getURL().'">'.$cat->getName().'</a></b>'.'</br>'; //If yes display it as bold (Currently Selected)
else //
echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a>'.'</br>'; //Otherwise display it as normal
}
}
Upvotes: 1