Joomla CategorySometimes you may want to style articles depending on which category they belong to. Here's how to load category specific CSS files...


First, add this to the very top of your template's index.php file (above the <!DOCTYPE> declaration)

$db = JFactory::getDBO(); $id = JRequest::getString('id');
$db->setQuery('SELECT #__categories.alias FROM #__content, #__categories WHERE #__content.catid = AND = '.$id);
$category = $db->loadResult();
if ($category == '') {$category = 'uncategorised';};

Then add this within the <head> tags on the same index.php file. (preferably after the last stylesheet is loaded).

<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/<?php echo $category ?>.css" type="text/css" />

This will simply check which category is currently used, then attempts to load a css file that uses the same category alias.
If the current page doesn't belong to a category (for example if it was a search page or a different type of component page) then the "uncategorised.css" file is loaded instead.

The last step is to create one css file for each of the categories you have (using the category alias as the filename) and save them in the template's css folder.


Hyde-Design 5/5 based on 625 0 5 customer reviews