Posts Tagged ‘Joomla’

A neat Joomla trick

Monday, January 26th, 2009

I’m in the process of making a Joomla site for the first time and I needed to be able to have different pages of the site have different styles. I looked around and wasn’t able to find any examples that actually worked for my needs. I found some info about the Page Class Suffix option, but nothing about how to really use it. I talked with Brian over at Heavy Robot, since he has actually made some Joomla sites and he whipped up this bit of code that fits my needs perfectly. Basically it uses the Page Class Suffix variable from the menu system as the class name on the body. You can then add any styles you need to your basic style sheet and have all sorts of different page styles.

Place this PHP block in the head of the index.php file of your template. I used the built in template editor, which also points out which template you are currently using.

<?php
     $menus = &JSite::getMenu();
     $menu = $menus->getActive();

     if (is_object( $menu ))
          $params = new JParameter( $menu->params );
          $pageclass = $params->get( 'pageclass_sfx' );
?>

 
Then replace the open body tag with this block, also in the index.php file. Or you can use just the part where the page class is written out if you want to have multiple classes on the body.

<body id="bd" class="<?php echo $pageclass;?>">

Now whenever you create a new menu item in the menu manager, add a value for the Page Class Suffix (under the Parameters (System) expando-box) for any pages that need different styles.