Magento: show custom attribute in body class

Magento: show custom attribute in body class

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: show custom attribute in body class

Magento: Dynamically Add Custom Category Attribute to Body Class

In Magento, adding custom attributes to the body class of category pages can be achieved without overriding core classes or manually adding static classes in the admin panel. This article will guide you through a clean and efficient approach using layout updates.

Understanding the Problem

You have a custom category attribute that you want to add as a dynamic body class on the frontend. The common approaches are:

  1. Overriding the CategoryController: You could override Magento’s core CategoryController and manually add the attribute value as a body class. However, this approach involves modifying core files which is against best practices.

    $root->addBodyClass($category->getMyAttribute());
    
  2. Manually Adding Classes in Admin: In the admin panel, you can add static classes directly to each category’s layout update XML. But this method doesn’t use your existing attribute and requires maintaining multiple places if you have many categories.

The Preferred Solution: Layout Update with Dynamic Attribute

The ideal solution is to create a custom layout update that dynamically adds your attribute value as a body class. This approach doesn’t require overriding core files and leverages your existing attribute data.

Here’s how to achieve this:

  1. Create a Custom Module: If you haven’t already, create a custom Magento module to house your code. For this example, let’s assume the module is named Custom_BodyClass.

  2. Update the Local.xml Layout File: In your new module, navigate to the view/frontend/layout directory and create a file called catalog_category_view.xml:

    <?xml version="1.0"?>
    <page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
    
        <reference name="root">
            <action method="addBodyClass">
                <className>
                    <!-- This is where we'll add the dynamic attribute value -->
                </className>
            </action>
        </reference>
    
    </page>
    
  3. Add a Custom Observer: To populate the className field, you need to create an observer that fetches your custom category attribute and sets it as the body class.

  4. Create a Helper Function (Optional but recommended): For reusability, create a helper function in your module to fetch the custom attribute value:

    <?php
    
    namespace Custom\BodyClass\Helper;
    
    use Magento\Framework\App\RequestInterface;
    use Magento\Framework\View\Element\AbstractBlock;
    
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
        const ATTRIBUTE_CODE = 'my_custom_attribute';
    
        protected $request;
    
        public function __construct(
            RequestInterface $request
        ) {
            $this->request = $request;
        }
    
        public function getCategoryAttributeValue()
        {
            if ($categoryId = (int) $this->request->getParam('id')) {
                return $this->_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId)->getData(self::ATTRIBUTE_CODE);
            }
            return null;
        }
    }
    
  5. Create the Observer: Now, create an observer that calls your helper function and adds the attribute value to the body class:

    <?php
    
    namespace Custom\BodyClass\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\View\Page\Config as PageConfig;
    
    class AddCategoryAttribute implements ObserverInterface
    {
        protected $helper;
        protected $pageConfig;
    
        public function __construct(
            Helper\Data $helper,
            PageConfig $pageConfig
        ) {
            $this->helper = $helper;
            $this->pageConfig = $pageConfig;
        }
    
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            if ($attributeValue = $this->helper->getCategoryAttributeValue()) {
                $this->pageConfig->addBodyClass($attributeValue);
            }
        }
    }
    
  6. Configure the Observer in your etc(module)_events.xml:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="controller_action_predispatch_catalog_category_view">
            <observer name="custom_body_class_observer"
                      instance="Custom\BodyClass\Observer\AddCategoryAttribute"/>
        </event>
    </config>
    

Conclusion

In this article, we demonstrated how to add a custom category attribute as a dynamic body class in Magento without overriding core classes or manually adding static classes. By following these steps and using layout updates with an observer, you can cleanly integrate your existing attribute data into the page’s structure.

Remember, always back up your files before making changes, and ensure your module is properly installed and enabled for this to take effect. For more information on Magento development best practices, refer to Laravel Company’s Magento Development Resources.

The key takeaway is to leverage Magento’s event-driven architecture and layout updates to achieve dynamic content manipulation without touching core files or creating redundant data.

Catalog Extensions Themes Admin

Our network