Question

I need to be able to add a class to the body tag based on the query string. There is no menu entry where I can set the page class suffix. Is there a way to do this programmatically?

Was it helpful?

Solution

A simple way is to have your template listen to the query and take the page class parameter from the url. Assuming a url of the form:

example.com?index.php&pageclass=blablaclass

Inside your template index.php put the following:

<?php
   $jinput = JFactory::getApplication()->input;
   $pageclass = $jinput->get('pageclass', 'default_value', 'cmd');
?>

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

OTHER TIPS

Ficticious URL:

http://www.mysite.com?body_tag_param=blah1;

PHP:

$param = '';
if(isset($_GET['body_tag_param'])) {
   $param = $_GET['body_tag_param'];
}
switch ($param) {
   case 'blah1' :
      $classname = 'hello1';
   break;
   case 'blah2' :
      $classname = 'hello2';
   break;
   default :
      $classname = 'default_class';
   break;
}
$script = <<<SCRIPT
   <script>
      $(document.body).addClass('{$classname}');
   </script>
SCRIPT;
$document = JFactory::getDocument();
$document->addCustomTag($script);

I haven't messed with Joomla or Mootools for a while because they are bulky but that should do it. Note, the other answer suggests to alter the entry point index.php file, that is a terrible idea. If you are not sure how the Joomla framework is put together, you might try to add this to your template.. Either way, you should never mess with the index.php file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top