0

I am trying to add a category id to css class on body tag but not able to find any way to do that.

With that category id on body class I can use different images for each category breadcrumbs using css.

At this moment I can't use default Magento 2 body classes because my website is multi language.

That example how than need to looks like.

<body class="current-category-id">
1

Create "after" plugin on \Magento\Catalog\Controller\Category\View::execute method

public function afterExecute(
    \Magento\Catalog\Controller\Category\View $subject, 
    \Magento\Framework\Controller\ResultInterface $result
) {
    $result->getConfig()->addBodyClass('current-category-' . $subject->getRequest()->getParam('id'));
}

Plase pay attention that Magento already have add uniqu body class to all categories in format:

->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))
->addBodyClass('category-' . $category->getUrlKey());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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