buy viagra
Home » Joomla!

Select template by Section in Joomla 1.5

14 January 2009 3 Comments

Problem

In Joomla 1.5, at present, there is no way to set the template applied to an article or category  based on the section that article is in. Templates can be set by menu item, but not by the overall section they reside in. This means that if multiple templates are being used, each time a new article is added, changes have to be made to the template setup too – very tedious. All I wanted was to be able to create an article or category, add it to a certain section, and know that it would be rendered/displayed using a particular template:

Solution

This took some searching and tweaking. I found a post in the Joomla developers forum about someone trying to do something similar, but their example code was broken. It was enough to get me started though.

What you need to do is: find and open application.php in the includes directory on the root of your Joomla 1.5 installation. Then find line 309 or thereabouts and look for this code:

309 // Allows for overriding the active template from the request
310 $template = JRequest::getCmd(‘template’, $template);
311 $template = JFilterInput::clean($template, ‘cmd’); // need to filter the default value as well

Insert AFTER the comment on line 309, but BEFORE line 310, add this code:

// Templates by Section hack – Begin

$eItemView = JRequest::getVar(‘view’);
$eItemId = JRequest::getVar(‘id’);

$sectionId = NULL;

$eItemId = (strpos($eItemId,”:”))? substr($eItemId,0,strpos($eItemId,”:”)) : $eItemId;

switch ($eItemView) {
case “article”:
$edb =& JFactory::getDBO();
$eQuery = ‘SELECT sectionid FROM #__content WHERE id LIKE ‘.$eItemId.”;
$edb->setQuery($eQuery, 0, 1);
$sectionId = $edb->loadResult();
break;
case “category”:
$edb =& JFactory::getDBO();
$eQuery = ‘SELECT section FROM #__categories WHERE id LIKE ‘.$eItemId.”;
$edb->setQuery($eQuery, 0, 1);
$sectionId = $edb->loadResult();
break;
case “section”:
$sectionId = $eItemId;
break;
}

// Edit the section id below, you can find it in the sections admin area.
if ($sectionId == “1″) {
$template = “rhuk_milkyway_red”; // Use the full template name.
}

// Add more if clauses if there are other templates.

// Templates by Section hack – End

Hopefully you can see what this does. It looks for the section id to use in 3 ways, depending on whether joomla is currently displaying a section root page, a category root page, or an article. It then looks to see whether that section id has had a specific template specified for it by name.

This hack works well for me, I only need to add the different template data once in application.php for a given site. Ideally someone needs to make an admin mod for this, so that sections and categories can be assigned different templates in the backend. Maybe one day I’ll make one, but not unless there’s demand :)

Any questions, corrections or thoughts, please comment below (no need to create an account or anything!)

3 Comments »

  • Michael Wolthers said:

    Brilliant, spent like 6 hours searching google for this.

    I love you! :D

  • admin said:

    Haha, no probs. Me too, which is why when I eventually found a way I thought I better share it!

  • Mychol Scully said:

    This looks extremely promising! I’ll post back with a link to the site I’m working on when I’ve got this code hack implemented.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.