Here is the code for getting category by category URL key.
The module name is: Magemonkeys_Categorydata
Create a block file: Categoryurl.php
<?php namespace MagemonkeysCategorydataBlockIndex; class Categoryurl extends MagentoFrameworkViewElementTemplate { { protected $filter; public function __construct( MagentoCatalogBlockProductContext $context, MagentoCatalogModelCategoryFactory $categoryFactory, array $data = [] ) { parent::__construct($context, $data); $this->categoryFactory = $categoryFactory; } public function getCategory($urlKey) { $categories = $this->categoryFactory->create()->getCollection() ->addAttributeToFilter('url_key', $urlKey) ->addAttributeToSelect(['entity_id']); return $categories; } }
Now you can get category by using this function getCategory($urlKey) in your phtml file
$urlKey = 'bags'; $category = $this->getCategory($urlKey); print_r($category->getData());
You can get an array of category data.