Change Magento body background for each cat ID

2.6k Views Asked by At

How to change Magento body background for each cat ID ? And Where to put the code??

I know that Cat id is :

Mage::getModel('catalog/layer')->getCurrentCategory()->getId()

2

There are 2 best solutions below

2
Nick On BEST ANSWER

You could do it off category name quite easily. Magento, by default, adds the following to the body tag:

<body class="catalog-category-view categorypath-electronics-cellphones category-cellphones">

It follows that you could define your stylesheet to work off the .category-{$category_name} class instead of a .category-{$category_id}, or similar. It would make more sense to the next person that comes along and edits your stylesheet too :)

Back to your question, if you would actually like to modify the code and actually used a category ID, then you'd have to modify the category controller, in the usual manner, and add:

$this->getLayout()->getBlock('root')->addBodyClass('category-' . $category->getId());

..in the Mage_Catalog_CategoryController::viewAction() at around the same point as the other classes are added.

0
freshwebs On

Here's a method that is flexible enough for a webstore owner to use in conjunction with their web designer (to apply the css rule).

Scenario: you want the same background image for a category & its children.

1) You apply a layout update to the parent category within the Category manager:

<reference name=”root”><action method=”addBodyClass”><className>caravan-motorhome-lighting</className></action></reference>

2) Set the parent category not to Use Parent Category Settings.

3) Set the child categories to Use Parent Category Settings

4) add body class declaration like so body.caravan-motorhome-lighting{background: url(..)}

You can find a fuller explanation Magento - changing the body class by category

I've used this technique in Magento 1.6.1.0 but should work with earlier versions where you can apply layout updates to a category.