使用模板覆盖修改 Joomla 主菜单的子菜单
我想在 Joomla 模板的主菜单中实现以下目标:
<ul class="topmenu">
<li><a class="nav_link" id="active" href="#">Home</a></li><span class="separator"></span>
<li><a class="nav_link" href="#">About Us</a></li><span class="separator"></span>
<li><a class="nav_link" href="#">Services</a>
<div class="subnav_wrapper">
<ul class="subnav">
<li><a class="sub_nav_link" href="#">Custom Software</a></li>
<li><a class="sub_nav_link" href="#">Software Solutions</a></li>
<li><a class="sub_nav_link" href="#">Mobile SMS</a></li>
<li><a class="sub_nav_link" href="#">Web Solutions</a></li>
<li class="last"><a class="sub_nav_link" href="#">ICT Consultancy</a></li>
</ul>
</div>
</li><span class="separator"></span>
</ul>
我已经覆盖了模块的 default.php 文件(我已将“default.php”从“modules\mod_mainmenu\tmpl”复制到“templates\mytemplate” \html\mod_mainmenu"。我不明白的是如何区分顶级 ul、li 和 a 元素以及子导航中的元素。例如,将类“last”添加到最后一个列表项在 subnav 中,我尝试了以下操作:
if ($node->name() == 'ul') {
foreach ($node->children() as $child)
{
if ($child->attributes('access') > $user->get('aid', 0)) {
$node->removeChild($child);
}
}
$children_count = count($node->children());
$children_index = 0;
foreach ($node->children() as $child) {
if ($children_index == $children_count - 1) {
$child->addAttribute('class', 'last');
}
$children_index++;
}
}
但是上面的内容也在顶级 ul 的最后一项中添加了类,
这是使用模板覆盖方法实现所需效果的方法吗?
I would like to achieve the following in my Joomla template's main menu:
<ul class="topmenu">
<li><a class="nav_link" id="active" href="#">Home</a></li><span class="separator"></span>
<li><a class="nav_link" href="#">About Us</a></li><span class="separator"></span>
<li><a class="nav_link" href="#">Services</a>
<div class="subnav_wrapper">
<ul class="subnav">
<li><a class="sub_nav_link" href="#">Custom Software</a></li>
<li><a class="sub_nav_link" href="#">Software Solutions</a></li>
<li><a class="sub_nav_link" href="#">Mobile SMS</a></li>
<li><a class="sub_nav_link" href="#">Web Solutions</a></li>
<li class="last"><a class="sub_nav_link" href="#">ICT Consultancy</a></li>
</ul>
</div>
</li><span class="separator"></span>
</ul>
I've already overriden the default.php file for the module (I've copied "default.php" from "modules\mod_mainmenu\tmpl" into "templates\mytemplate\html\mod_mainmenu". What I don't get is how I can differentiate between the top-level ul, li and a elements and those in the subnav. For example, to add the class "last" to the last list item in the subnav, I've tried the following:
if ($node->name() == 'ul') {
foreach ($node->children() as $child)
{
if ($child->attributes('access') > $user->get('aid', 0)) {
$node->removeChild($child);
}
}
$children_count = count($node->children());
$children_index = 0;
foreach ($node->children() as $child) {
if ($children_index == $children_count - 1) {
$child->addAttribute('class', 'last');
}
$children_index++;
}
}
But the above adds the class also in the last item of the top-level ul.
Is a way to achieve the desired effect using the template override method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定菜单的模板覆盖。您可以查看 http://extensions。 joomla.org/extensions/struct-a-navigation/menu-systems/tree-menus/163 这是一个很棒的自定义菜单模块。
或者,如果您只想设置菜单项的样式,您可以考虑在 CSS 中使用 ul li:last-child 。请注意,并非所有浏览器都完全支持这一点。
Not sure about the template overriding of menus. You could look at http://extensions.joomla.org/extensions/structure-a-navigation/menu-systems/tree-menus/163 which is a great module for customising menus.
Alternatively, if you want to merely style the menu items you could look into using ul li:last-child in your CSS. Note this is not fully supported across all browsers.
为了将
last
和first
类指定到当前 2.5.4 版本的 Joomla 中分层菜单分支的最后一个和第一个项目,我将添加到default.php
(它从modules\mod_mainmenu\tmpl\
复制到templates\mytemplate\html\mod_mainmenu\
)此代码:之后:
插入:
并替换:
with:
有可能去掉,例如在顶级的最后一项中添加一个
last
类ul
– 可以替换:为:
For appointment a
last
andfirst
classes to the last and first items of branches of the hierarchical menu in current 2.5.4 version of Joomla, I'm add todefault.php
(it's copied frommodules\mod_mainmenu\tmpl\
totemplates\mytemplate\html\mod_mainmenu\
) this code:after:
insert:
and replace:
with:
There is the possibility of get rid of such as adding a
last
class in the last item of the top-levelul
– can be replaced:with: