以编程方式改变身体类别

时间:2014-04-19 16:58:26

标签: joomla joomla3.0

我需要能够根据查询字符串向body标签添加一个类。没有菜单条目,我可以设置页面类后缀。有没有办法以编程方式执行此操作?

2 个答案:

答案 0 :(得分:2)

一种简单的方法是让您的模板监听查询并从网址中获取页面类参数。 假设形式为url:

example.com?index.php&pageclass=blablaclass

在模板index.php中添加以下内容:

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

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

答案 1 :(得分:-2)

虚构网址:

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);

我没有和Joomla或Mootools搞砸了一段时间,因为它们很笨重但应该这样做。注意,另一个答案建议改变入口点index.php文件,这是一个可怕的想法。如果您不确定如何将Joomla框架放在一起,您可以尝试将其添加到模板中。无论哪种方式,您都不应该使用index.php文件。

相关问题