[英]get body class by active parent menu item
example:例子:
menuitem 1菜单项 1
menuitem 1.1菜单项 1.1
menuitem 1.2菜单项 1.2
menuitem 1.2.1菜单项 1.2.1
menuitem 2菜单项 2
menuitem 3菜单项 3
I now have:我现在有:
<?php $active = JFactory::getApplication()->getMenu()->getActive();?>
<body class="<?php echo $active->alias; ?> ">
So I load the body class from the active menu item.所以我从活动菜单项加载主体 class 。 I'd like that to stay the same.我希望保持不变。 The thing I need is to hide certain menu item's when for example menuitem 1 is active.我需要的是在例如 menuitem 1 处于活动状态时隐藏某些菜单项。 So when that happens I need to assign a class to menuitem 2.因此,当发生这种情况时,我需要将 class 分配给 menuitem 2。
That way when I navigate down 1.1 it keeps the menu items hidden.这样,当我向下导航 1.1 时,它会隐藏菜单项。
Found a solution:找到了解决方案:
<?php
$menu =& JSite::getMenu();
$active = $menu->getActive();
$class = extractpath($active->route);
function extractpath($string){
preg_match('/^[a-zA-Z0-9\-_]+/',$string,$m);
return $m[0];
}
?>
<body<?php if ($class) echo ' class="' . $class . '"'; ?>>
To get the ID of the parent - $menu->getActive()->parent获取父级的 ID - $menu->getActive()->parent
To get the Title of the parent - $menu->getItem([id])->name获取父级的标题 - $menu->getItem([id])->name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.