Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

magento

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.

{{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 .

like image 802
molleman Avatar asked Mar 02 '12 13:03

molleman


People also ask

How can I call Phtml file in Magento2?

login to magento admin. Open any cms page and write block code in content section. After using the block code phtml file will be called on cms page. If you want to call phtml file on all cms pages then you can create a layout file to achieve this.

What is Phtml file in Magento2?

phtml : is a template file of your module. Now, create cart.phtml file at location: app/code/Webkul/Hello/view/frontend/templates folder <h1>Shopping Cart</h1> //write your stuff here. Magento2. Bulbul 6 Badges 16 July 2021.


1 Answers

Inside the list.phtml block template you can get the category name with

<?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.

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

<?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.

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

<?php echo Mage::getModel('catalog/category')->load($this->getCategoryId())->getName() ?>
like image 54
Vinai Avatar answered Oct 30 '22 11:10

Vinai