Joomla: How to change template on a specific article

Kratz picture Kratz · Jul 17, 2010 · Viewed 20.2k times · Source

Is there a way to change the template on a specific article only? Note that it should work without linking the article to any menu.

Answer

silvo picture silvo · Jul 18, 2010

If you want the template override not to depend on the menu position than the standard joomla way of assigning a different template to a menu will not work. You will need to get your hands dirty and write some custom code. You will need to use the article_id as a trigger for template switch.

I did something like that at work but don't remember now how exactly this is achieved. I will post my code here as soon as I locate it.

EDIT: Found the code :)

You need to edit the file /includes/application.php, specifically the getTemplate() method. At the end of this method, just before:

// Fallback template
if (!file_exists(JPATH_THEMES.DS.$template.DS.'index.php')) {
  $template = 'rhuk_milkyway';
}

you can add your condition for applying a custom template, like so:

//CUSTOM TEMPLATE FOR THE ARTICLE 13
if (JRequest::getVar('id')=='13' && JRequest::getVar('option')=='com_content') {
  $template = $custom_template_name;
}

This will apply the custom template which name is inside the $custom_template_name to article with id=13. You can also use it to apply a different template to components, like I did with simplecaddy:

//TEMPLATE FOR SIMPLECADDY
if (JRequest::getVar('option')=='com_caddy'){
  $template = 'shop';
}