In the past couple months I’ve worked on several Joomla 1.5.x sites. I’m starting to develop a bag of tricks. This trick will allow you to assign a unique css class or id for each page. I’ve used this technique to assign different header graphics to individual pages. I’m far from calling myself a “Joomla Guy,” so any input or tweaks would be greatly appreciated.
My approach is to parse the http variables with PHP and create a unique identifier. I created a function called getclass and put it at the top of my template’s index.php file. There is probably a better place to put helper functions, but I don’t know where that is yet. The function looks like this:
function getclass() {
$id = split (':', $_GET['id']);
$class = “”;
if (isset($_GET['option'])) {
$class = $class . $_GET['option'];
}
if (isset($_GET['view'])) {
$class = $class . “_” . $_GET['view'];
}
if ($id[0] > 0) {
$class = $class . “_” . $id[0];
}
return $class;
}
Now to use the class I’m adding it to the body element like so:
<body class="<?= getclass(); ?>" >
You can use firebug to inspect the body element and get a page’s class. To use it in your style sheet you can do something like this:
/* Default header */
#header {
height: 100px;
width: 900px;
background-image: url(../images/default.jpg);
}
body.com_content_category_6 #header {
background-image: url(../images/header1.jpg);
}
body.com_contact_contact_1 #header {
background-image: url(../images/headers/header2.jpg);
}
body.com_content_section_1 #header {
background-image: url(../images/headers/header3.jpg);
}
This method has it’s limits and I’m not 100% happy with it. For instance it would be cool to be able to pull a page’s section or category id so you could do per section or category specific header. The problem is that these variables don’t get passed through the URL as variables. I saw some examples where people were writing custom SQL queries to do this, but I haven’t had a project to warrant putting that much time into it. Also it seems to me that there has to be some sort of API call, like $thisPage->getCategoryid().