I want to get category name using multiple category ids.

I have two category id like :- 3 or 4.Those two id using i am want to category name.

Please help me how to achieve this.


  • for example?? What ids do you have? is it in an array or what? Maybe explain a little bit more. Dec 19, 2020 at 14:15
  • @KlausMikaelson i have update my question. please check. Thanks Dec 19, 2020 at 15:55
  • You may already have checked this but still stackoverflow.com/a/38692903 . Dec 19, 2020 at 16:42
  • yes,But how to use multiple id? Dec 19, 2020 at 16:43

3 Answers 3

namespace Company\Module\Block;

class CustomBlock extends \Magento\Framework\View\Element\Template 
    protected $_categoryFactory;    

    public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Framework\View\Element\Template\Context $context,
    ) {
        $this->_categoryFactory = $categoryFactory;

    public function getCategoryName($catIds)
        $catNames = [];
        foreach ($catIds as $key => $catId) {
         $category = $this->_categoryFactory->create()->load($catId);
         $catNames[$catId] = $category->getName();
        return $catNames;
    public function getAllCatName()
      $catIds = array(3,4);
      $categoryNames = $this->getCategoryName($catIds);

In phtml file you can call $block->getAllCatName(); Or if you have ids 3,4 in array phtml like $catIds = array(3,4); you can directly call $categoryNames = $block->getCategoryName($catIds);

  • Yes,this answer help me. THANKS Dec 19, 2020 at 17:17
  • Hi, How to get product collection by multiple category id. Dec 20, 2020 at 9:45
  • I will check that also in my free time. But you should ask a new question. Someone will give the answer surely. People will not come here and see your comment but they will see new questions so that would get you a fast solution. Dec 20, 2020 at 14:34

where you need to get the categories name? phtml? block? Model?

for example, in a phtml file you can inject a viewModel and in that class you can get the categories name.

To get the categories name by multiple ids, you can made a collection filtering by entity_id and selecting only the "name" attribute, or you can instantiate the CategoryRepository and with a loop you can load the categoryModel for every id.

  • yes, i want to category name in phtml file.But how ? Dec 19, 2020 at 16:42
  • please can you add php code? Dec 19, 2020 at 16:46

ok, so you need to edit the layout xml (if you need this info on the product page, you need to edit the catalog_product_view.xml). Find the block tag and add this code:

<arguments><argument name="viewModel" xsi:type="object">Vendor\Module\ViewModelFolder\ViewModelClass</argument></arguments>

This class must implement the ArgumentInterface.

In his constructor you can instantiate a CategoryRepository class, like:

protected $categoryRespository;

public function __construct(\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository) 
    $this->categoryRepository = $categoryRepository;

Or you can instantiate the CollectionFactory:

protected $categoryCollectionFactory;

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
) {
    $this->categoryCollectionFactory = $categoryCollectionFactory;

After this you need to create a method that accept an array with the ids:

public function getCategoryNameByIds(array $ids = [])
    $names = [];
    //for categoryRepository
    foreach ($ids as $id) {
        /** @var \Magento\Catalog\Model\Category $category */
        $category = $this->categoryRepository->get($id);
        $names[$id] = $category->getName();

    //for collectionFactory
    $categoryCollection = $categoryCollectionFactory->create();
    $categoryCollection->addFieldToFilter('entity_id', [in => $ids]);

    // either call getItems() and iterate over it, or do whatever you need
    foreach ($categoryCollection->getItems() as $category) {
        /** @var \Magento\Catalog\Model\Category $category */
        $names[$category->getId()] = $category->getName();
    return $names;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.