I have created a new category custom attribute. It works fine, the attribute is saved when I save the category.

I'd like to use this attribute in the navigation tree logic so that I can apply some changes to the tree depending on its value.

The problem is that I can't access this attribute value from here: Magento\Theme\Block\Html\Topmenu (using a preference, of course). The point is that I need to edit the html markup, but the $child elements in there don't contain this category data.

How can I add that category attribute data to the nodes?


2 Answers 2


Default magento menu render from Magento\Catalog\Plugin\Block\Topmenu. This class is plugin class of Magento\Theme\Block\Html\Topmenu.

If you want to customized then you have to work on this class.

First you have to add your custom attribute to collection of tree mean \Magento\Catalog\Plugin\Block\Topmenu::getCategoryTree.

Have to add field your field collection->addAttributeToSelect('{YOUR_ATTRIBUTE_CODE}'); like collection->addAttributeToSelect('name'); then you can access that attribute to desire function of that class.


create etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Model\Category" type="VendorName\ModuleName\Model\CategoryCustomAttribute" />

then create CategoryCustomAttribute.php


namespace VendorName\ModuleName\Model;

class CategoryCustomAttribute extends \Magento\Catalog\Model\Category
    public function getCatgCustomAttribute()
        return $this->getCustomAttribute('your_attribute_code'); // pass your attribute code here           

then just call this function getCatgCustomAttribute()in your template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.