0

How can I change the page template when viewing a certain category page?

So instead of the certain category page using page/1column.phtml, I'm trying to make it use a custom one, page\blah.phtml, and all other categories should still use page/1column.phtml.

I'm also wondering where I should put the following code, as I've tried putting it in a template file with no luck, I'm guessing it needs to go in a controller or a block PHP file, but I'm not sure which one.

$root = $this->getLayout()->getBlock('root');
$template = 'page/blah.phtml';
$root->setTemplate($template);
4

In the below example, I'm changing the view of Product Detail Page. So you can get the idea about it. Make sure to change parameters according to your need. Add below code in your layout file. If you're making custom module then it'll be on your custom module layout file. You can get below code in catalog.xml in your theme.

<catalog_product_view>
    <reference name="content"> 
 <block type="blah/blah" name="blah" template="page/blah.phtml" />  
    </reference>
</catalog_product_view>

For, the second query, it should be in controller.But, I don't think that code will be needed if you use the below code in controller.

 $this->loadLayout(); or // $this->loadLayout('root');
 $this->renderLayout();
| improve this answer | |
1

You can do this by layout handler ta magento

<CATEGORY_MYCATID>
<reference name="root">
<action name="setTemplate">page/blah.phtml</action >
</reference>
</CATEGORY_MYCATID>

Suppose Category your category id is 75 then only for this category ,you can set page set template.

<CATEGORY_75>
<reference name="root">
<action name="setTemplate">page/blah.phtml</action >
</reference>
</CATEGORY_75>
| improve this answer | |
  • Thanks but where do I put that code? I have tried adding it to app/design/frontend/default/mysite/layout/page.xml but it didn't seem to work. Also is there a layout handle that can target all child categories (recursively) of a certain category? – Mex Mar 26 '15 at 12:56
  • @Mex See this extension I created: github.com/ProxiBlue/GlobalHandle which injects more handles into the layout. You should be able to adjust that fairly easily to inject a handle for a categories parent category, which will give you what you need. – ProxiBlue Mar 26 '15 at 15:15
  • @ProxiBlue Thanks, I have already solved my issue by using a different CSS class name on the body tag. I will bookmark your module, the event name you're using seems like the one I needed, as well as adding handles. – Mex Mar 26 '15 at 16:48

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.