简体   繁体   English

获取当前类别的父类别

[英]get parent category of current category

I'm working on a website where my clients want me to display the parent category of current category. 我在一个网站上工作,我的客户希望我显示当前类别的父类别。 I know how to display the category of current product. 我知道如何显示当前产品的类别。

Mage::registry('current_category')->getName();

But I'm struggling to get the parent category of that category. 但是我在努力获得该类别的父类别。 Please help me regarding this. 请帮助我。 I'm a budding developer. 我是一个崭露头角的开发人员。

To get Parent Category Id you must know the current category Id, for that you need to write 要获取父类别ID,您必须知道当前的类别ID,为此您需要编写

<?php
$_cat = new Mage_Catalog_Block_Navigation();
$curent_cat = $_cat->getCurrentCategory();
$curent_cat_id = $curent_cat->getId();?>

Now you can get Parent Category Id. 现在,您可以获得父类别ID。 write the code given below to get the Id 编写下面给出的代码以获取ID

<?php $parentId=Mage::getModel('catalog/category')->load($curent_cat_id)->getParentId();
echo $parentId; // $parentId will print your current category's parent Id
?>

Please refer this blog it contains the necessary information to achieve your requirement 请参考博客,其中包含满足您要求的必要信息

Best of Luck :) 祝你好运:)

You can simply use this short code to get the parent category: 您可以简单地使用以下短代码来获取父类别:

$parentId = Mage::registry('current_category')->getParentId();
$parent = Mage::getModel('catalog/category')->load($parentId);
exit($parent->getName());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM