0

我想根据我正在查看的页面向我的正文标签(或 div)添加一个页面类。我找到了一个教程,但它不能 100% 工作,它会在每个页面上添加 id="default"。我需要每个页面生成一个特殊的类或 id。

这就是我到目前为止所拥有的,插入到我的模板的 index.php 中:

<?php
  $itemid = JRequest::getVar('Itemid');
  $menu = &JSite::getMenu();
  $active = $menu->getItem($itemid);
  $params = $menu->getParams( $active->id );
  $pageclass = $params->get( 'pageclass_sfx' );
?>
</head>
<body id="<?php echo $pageclass ? htmlspecialchars($pageclass) : 'default'; ?>">

PS。我的搜索引擎友好 URL 不起作用,所以我不得不关闭它们。

4

2 回答 2

1

此示例是 Joomla 2.5.x 及更高版本的代码:

<?php
  $app = JFactory::getApplication();
  $menu = $app->getMenu()->getActive();
  $pageclass = ''; // Notice how the variable is empty first. Then place the code below.

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

参考:http ://docs.joomla.org/Using_the_Page_Class_Suffix_in_Template_Code

于 2014-09-14T03:25:27.850 回答
0

以下是我向所有页面添加页面类的方法:

在我的 index.php 文件的开头,我添加了以下 php:

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

然后,我将以下内容用于新的开始正文标记:

<body class="<?php echo $pageclass ? htmlspecialchars($pageclass) : 'default'; ?>">

如果未设置页面类,则默认情况下会将正文类设置为“默认”(见下文)。

完成上述操作后,更改 body 类就很容易了。在 Joomla 管理员中,打开要设置页面类的菜单项,单击高级选项并向下滚动/展开页面显示选项。在Page Class中输入班级名称,就是这样!

于 2013-08-13T00:28:01.057 回答