Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joomla: How to change template on a specific article

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.

like image 492
Kratz Avatar asked Jul 17 '10 01:07

Kratz


2 Answers

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';
}
like image 168
silvo Avatar answered Oct 13 '22 00:10

silvo


You should really try to stay away from hard coding anything in to the template if it can be avoided. Not sure why you would specify that the article not be linked from a menu. The easiest way to accomplish this without having to write and code is to create a new menu, then add a menu item that links to the article you want to specify the template for. You don't have to put the menu in a module anywhere so it will never show up on the site, but it will show up in the menu assignment in the template manager.

You can do this with single articles, categories, sections, or even components. As long as you have a menu link to associate the template to. I always create an Admin only menu to put links that are needed to run the site, but do not need to be accessed by users.

like image 34
Brent Friar Avatar answered Oct 12 '22 23:10

Brent Friar