1

It is code to show category name in product listing.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$categories = $_product->getCategoryIds(); /*will return category ids array*/
foreach ($categories as $category) {
    $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
    echo $cat->getName();
}
?>

How would I get a category with a link?

  • Try this $cat->getUrl() – Dinesh Yadav Nov 21 '17 at 4:57
  • @Dinesh thanks. can get link with $cat->getUrl(). – Nang Thaint Nov 21 '17 at 6:49
0

Try this,

<?php 
   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    echo $category->getId();
    echo $category->getName();
    echo $category->getUrl();
?>
  • <code> $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categories = $_product->getCategoryIds(); /*will return category ids array*/ foreach($categories as $category){ $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category); echo "<br/>"; echo "<a href='".$cat->getUrl()."' class='".$cat->getId()."'>".$cat->getName()."</a>"; } </code> – Nang Thaint Nov 21 '17 at 6:47
  • Did you try my code ???????? – Magento 2 Nov 21 '17 at 7:04
  • yes. not working well. – Nang Thaint Nov 21 '17 at 7:08
  • Not working well meaning – Magento 2 Nov 21 '17 at 7:23
0
namespace Meetanshi\HelloWorld\Block;
use Magento\Catalog\Api\CategoryRepositoryInterface;
class Meetanshi
    protected $_storeManager;
    protected $categoryRepository;
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        CategoryRepositoryInterface $categoryRepository
    ) {
        .........
        $this->_storeManager = $storeManager;
        $this->categoryRepository = $categoryRepository;
    }

     public  function getCategory()
    {
            $category = $this->categoryRepository->get($categoryId, $this->_storeManager->getStore()->getId());

        return $category->getUrl();
    }
}

Call this block from listing page by passing category id like below

$block->getCategory('YOUR_CATEGORY_ID');

Thanks

0

There are 2 ways to get category Data:

1) Using Object Manager:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$registry = $objectManager->get('\Magento\Framework\Registry');
$currentCategory = $registry->registry('current_category');
echo $currentCategory->getName() . '<br />';
echo $currentCategory->getUrl() ;

2) Or using Block

i) Create Block:

app/code/Allin/HelloWorld/Block/HelloWorld.php

<?php
namespace Allin\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
        protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {        
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCurrentCategory()
    {        
        return $this->_registry->registry('current_category');
    }

    public function getCurrentProduct()
    {        
        return $this->_registry->registry('current_product');
    }    

}
?>

ii) Get current category in any template (.phtml) file

// print current category data
$currentCategory = $block->getCurrentCategory();
echo $currentCategory->getName() . '<br />';
echo $currentCategory->getUrl() . '<br />';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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