cancel
Showing results for 
Search instead for 
Did you mean: 

How to sort by name all categories in my layered navigation?

How to sort by name all categories in my layered navigation?

I need to have all categories and subcategories ordered by name.

How can I do it? Do I have to modify this file?

`app/design/VENDOR/THEME/Magento_Catalog/templates/navigation/left.phtml`

I've tried:

```
$_categories = $block->getCurrentChildCategories()->addAttributeToSort('name','ASC');
$_categories = $block->getCurrentChildCategories()->setOrder('name', 'ASC');
```

I've tried this solution to
https://magento.stackexchange.com/questions/316903/magento-2-3-sort-categories-alphabetically/354750...

But none of the tests are working for me.

Could you help me? Please!

thanks

2 REPLIES 2

Re: How to sort by name all categories in my layered navigation?

 

foreach ($_categories as $_category){ <br> $categories[$_category->getName()] = $_category->getUrl();<br> ksort($categories, SORT_STRING);<br> }

Re: How to sort by name all categories in my layered navigation?

I tried your solutions but it don't works

In my file
app/design/VENDOR/THEME/Magento_Catalog/templates/navigation/left.phtml

<?php foreach ($_categories as $_category){  $_categories[$_category->getName()] = $_category->getUrl(); ksort($_categories, SORT_ASC);  echo $block->escapeHtml($_category->getName()); } ?>

I've insered your code but it give me this error

Error: Cannot use object of type Magento\Catalog\Model\ResourceModel\Category\Collection\Interceptor as array in /home/customer/www/site.it/public_html/app/design/frontend/VENDOR/THEME/Magento_Catalog/templates/navigation/left.phtml.

 

I'm doing something wrong?