February 11, 2015

7:52 am
In this article, we are going to discuss about How to add the category name to the body class in Magento. The Magento eCommerce platform serves more than 110,000 merchants worldwide and is supported by a global ecosystem of solution partners and third-party developers. Magento is a feature-rich, open-source, enterprise-class platform that offers merchants a high degree of flexibility and control over the user experience, catalog, content and functionality of their online store.

Sometimes you would like to modify the page according to the category. For example, you want to change the banner, background or other things.

Step 1

you can add the next code snippet to the /template/page/1column.phtml or /template/page/2columns-left.phtml or anywhere else you want:

<?php
$class = '';
$currentCategory = Mage::registry('current_category');

if ($currentCategory) {
    $parentCategories = $currentCategory->getParentCategories();
    $topCategory = reset($parentCategories);
    $class .= $topCategory->getUrlKey();
}
?>

Step 2

Next, append the $addClass variable to the body class like this:

<body <?php echo $this->getBodyClass()?' class="'.$this->getBodyClass() . " " . $class . '"':'' ?> >

0 comments:

Post a comment