php 如何从 Magento 中的 list.phtml 获取类别名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9534236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the category name from list.phtml in Magento
提问by molleman
So i am trying to display a custom list.phtml file from within a block. thats fine i am able to display all the products with category id 6.
所以我试图从一个块中显示一个自定义的 list.phtml 文件。没关系,我可以显示类别 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. i just need the name of the category for the id =6 .
以上工作正常。但是现在我想访问类别 id 6 的名称,我将如何从 list.phtml 或什至从不同的块中执行此操作。我只需要 id =6 的类别名称。
回答by Vinai
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.
在这种情况下,当前类别由调用中的catalog/product_list
块在图层上设置_getProductCollection()
。
Inside the CMS page content there is no way I know of to access the category name directly.
From a different block getting the category name might be more involved. You can try
在 CMS 页面内容中,我无法直接访问类别名称。
从不同的块获取类别名称可能更复杂。你可以试试
<?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. If you don't care about that you can always get the category name with
编辑:所有这些都是假设您想在不再次指定类别 ID 的情况下获取类别名称。如果你不关心,你总是可以得到类别名称
<?php echo Mage::getModel('catalog/category')->load($this->getCategoryId())->getName() ?>