{"id":1580,"date":"2021-02-09T05:32:09","date_gmt":"2021-02-09T05:32:09","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-all-category-urls-in-magento-2\/"},"modified":"2025-07-17T09:36:29","modified_gmt":"2025-07-17T04:06:29","slug":"get-all-category-urls-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-all-category-urls-in-magento-2\/","title":{"rendered":"How To Get All Category URLs in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0store categories make it easy for the visitors to find the exact desired product quickly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the programmatic solution to&nbsp;<em><strong>get all category URLs in Magento 2<\/strong><\/em>. With this solution, the admin can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-all-categories\/\" target=\"_blank\" rel=\"noreferrer noopener\">get the list of all categories<\/a>&nbsp;URLs in the store and then use it in multiple ways according to the business requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you want to implement HTML sitemap in Magento 2 store. HTML sitemap is sort of a blueprint of the online store. It makes navigation easy. The admin can also use HTML sitemap to keep the track of all the URLs in the Magento 2 store. Using the below code, one can list the category URLs in the HTML sitemap.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, when the Magento 2 store gradually expands, the admin may want a handy list of category URLs while adding new or updating the existing categories.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In all such scenarios, use the below solution to get all categories URLs in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Get All Category URLs in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Use the below code in&nbsp;<strong>registration.php<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nuse Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(\n    ComponentRegistrar::MODULE,\n    'Vendor_Module',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/etc\/<\/strong>&nbsp;and paste the below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\">\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>CategoryPageUrls.php&nbsp;<\/strong>at&nbsp;<strong>app\/code\/Vendor\/Module\/Helper\/&nbsp;<\/strong>and use the below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php namespace Vendor\\Module\\Helper; use Magento\\Catalog\\Model\\ResourceModel\\Category\\CollectionFactory as CategoryCollectionFactory; use Magento\\Framework\\App\\Config\\ScopeConfigInterface; use Magento\\Framework\\App\\Helper\\AbstractHelper; use Magento\\Framework\\App\\Helper\\Context; class CategoryPageUrls extends AbstractHelper { const CATEGORY_URL_SUFFIX = 'catalog\/seo\/category_url_suffix'; \/** * @var CategoryCollectionFactory *\/ private $categoryCollectionFactory; public function __construct(Context $context, CategoryCollectionFactory $categoryCollectionFactory) { $this->categoryCollectionFactory = $categoryCollectionFactory;\n \n        parent::__construct($context);\n    }\n \n    public function getCategoryUrls()\n    {\n        $categoryUrls = [];\n        $categories = $this->categoryCollectionFactory->create();\n        $categories->addAttributeToSelect('url_path');\n        foreach ($categories as $category) {\n            if (!is_null($category->getData('url_path'))) {\n                $categoryUrls[] = rtrim($this->getUrl($category->getData('url_path')), '\/') .\n                    $this->getConfigData(self::CATEGORY_URL_SUFFIX);\n            }\n        }\n \n        return $categoryUrls;\n    }\n \n    public function getConfigData($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = null)\n    {\n        return $this->scopeConfig->getValue($path, $scope, $scopeId);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not forget to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Related Posts:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. <a href=\"https:\/\/meetanshi.com\/blog\/create-new-root-category-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create a New Root Category in Magento 2<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. <a href=\"https:\/\/meetanshi.com\/blog\/get-categories-from-a-product-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Get Categories From a Product in Magento 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0store categories make it easy for the visitors to find the exact desired product quickly. Here\u2019s the programmatic solution to&nbsp;get all category URLs in&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1580","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1580"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1580\/revisions"}],"predecessor-version":[{"id":17761,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1580\/revisions\/17761"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}