You can get category collection by category id by below code snippet,
<?php
namespace Rbj\Category\Block;
class Category extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
array $data = []
) {
$this->categoryRepository = $categoryRepository;
parent::__construct($context, $data);
}
/* $categoryId as category id */
public function getCategoryById($categoryId){
try {
return $category = $this->categoryRepository->get($categoryId);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
return ['response' => 'Category Not Found'];
}
}
}
Call From template file, Pass argument as Category id,
$categoryId = 5; // category id $getCategory = $block->getCategoryById($categoryId); echo $getCategory->getName();echo "<br>"; echo $getCategory->getUrlKey();echo "<br>"; echo $getCategory->getIsActive();echo "<br>"; echo "<pre>";print_r($getCategory->getData());
