문제

How can I get the top level parent category (just after Default Category) from current category object? I want the Category Name and Category URL of that parent category to show in the subcategories.

  • Default Category
    • Cat A
      • Cat B
        • Cat C

How can I get Cat A's Name and URL in Cat B and Cat C objects?

도움이 되었습니까?

해결책

$category->getPath() will return the ids of all categories from the tree root to the current one separated by slash (/). Here is an example: 1/2/56/124/543. The first one is the 'root of roots'. The second one is the catalog root (default category). The rest of them are simple categories. So you can do something like this.

$path = $category->getPath();
$ids = explode('/', $path);
if (isset($ids[2])){
    $topParent = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($ids[2]);
}
else{
    $topParent = null;//it means you are in one catalog root.
}

Now you can get the name and url like this:

if ($topParent){
    $name = $topParent->getName();
    $url = $topParent->getUrl();
}

다른 팁

Try this

$level = $this->getCurrentCategory()->getParentCategory()->getLevel();
if($level > 1){                 
    echo $this->getCurrentCategory()->getParentCategory()->getName();
    }
    else{
    echo $this->escapeHtml($_category->getName());
    }

Simply copy and paste this code :)

You could use getParentCategories() on the current category and then call array_pop to get the last element. Do it twice to get the second last element.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top