Create the block file in your module.
<?php
namespace MagemonkeyCategoryBlock;
class Category extends MagentoFrameworkViewElementTemplate
{
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCatalogApiCategoryRepositoryInterface $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 (MagentoFrameworkExceptionNoSuchEntityException $e) {
return ['response' => 'Category Not Found'];
}
}
}
?>
Put this code in your template file and pass the category id in getCategoryById function.
<?php $categoryId = 10; // 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()); ?>

