如何在Magento的类别页面上仅显示父类别和子类别?
单击子类别时,导航必须保持不变。
实施例: 如果我有
第1类
点击任何子类别(比如subcat 1)后,我想要相同的,即:
第1类
我现在得到了什么:
<?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> ';
}
}
?>
但当然这不正确但我找不到一个好的解决方案,以便当我按下subcat 1时类别1保持不变。
如果选定的子类别为粗体,那将是非常好的。
答案 0 :(得分:1)
在您的代码中,代替
<?php
if (Mage::registry('current_category'))
{
echo Mage::registry('current_category')->getName();
}
?>
尝试使用
<?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();
}
?>
它应该有用。
修改: 以下是您的完整解决方案:
$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
}
}