简体   繁体   English

如何从Magento中的list.phtml获取类别名称

[英]How to get the category name from list.phtml in Magento

So i am trying to display a custom list.phtml file from within a block. 所以我试图在一个块中显示一个自定义list.phtml文件。 thats fine i am able to display all the products with category id 6. 多数民众赞成我能够显示所有类别为id 6的产品。

{{block type="catalog/product_list" category_id="6" template="catalog/product/list.phtml"}}

the above works fine. 以上工作正常。 But now i want to get access to category id 6's name, how would i go about doing this from within list.phtml or even from within a different block. 但是现在我想要访问类别id 6的名称,我将如何从list.phtml中或甚至在不同的块中执行此操作。 i just need the name of the category for the id =6 . 我只需要id = 6的类别名称。

Inside the list.phtml block template you can get the category name with 在list.phtml块模板中,您可以获取类别名称

<?php echo $this->getLayer()->getCurrentCategory()->getName() ?>

In this case the current category is set on the layer by the catalog/product_list block in the _getProductCollection() call. 在这种情况下,当前类别由_getProductCollection()调用中的catalog/product_list块在图层上设置。

Inside the CMS page content there is no way I know of to access the category name directly. 在CMS页面内容中,我无法直接访问类别名称。
From a different block getting the category name might be more involved. 从不同的块获取类别名称可能更复杂。 You can try 你可以试试

<?php echo Mage::getSingleton('catalog/layer')->getCurrentCategory()->getName() ?>

Of course it might be the case that there is no current category might set on the layer instance, so make sure to check for that to avoid ugly errors. 当然可能的情况是,层实例上可能没有设置当前类别,因此请务必检查以避免出现难看的错误。
Basically, if the catalog/product_list product list block's _beforeToHtml() method has been executed the current category will be set on the layer. 基本上,如果已执行catalog/product_list产品列表块的_beforeToHtml()方法,则将在该层上设置当前类别。

EDIT: All this is assuming you want to get the category name without specifying the category ID again. 编辑:所有这一切都假设你想获得类别名称而不再指定类别ID。 If you don't care about that you can always get the category name with 如果您不关心,您可以随时获取类别名称

<?php echo Mage::getModel('catalog/category')->load($this->getCategoryId())->getName() ?>

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

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