使用模板覆盖修改 Joomla 主菜单的子菜单

发布于 2024-09-14 11:54:20 字数 1816 浏览 2 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-09-21 11:54:20

不确定菜单的模板覆盖。您可以查看 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.

蓝戈者 2024-09-21 11:54:20

为了将 lastfirst 类指定到当前 2.5.4 版本的 Joomla 中分层菜单分支的最后一个和第一个项目,我将添加到 default.php(它从modules\mod_mainmenu\tmpl\复制到templates\mytemplate\html\mod_mainmenu\)此代码:

之后:

defined('_JEXEC') or die;

插入:

$last_items = array();
foreach( array_reverse( $list, true ) as $v ) {
    if( ! isset( $last_items[$v->parent_id] ) )
        $last_items[$v->parent_id] = $v->id;
}
$first_start = true;

并替换:

if ($item->deeper) {
    $class .= ' deeper';
}

with:

if( $first_start ) {
    $class .= ' first';
    $first_start = false;
}
else if( in_array( $item->id, $last_items ) ) {
    $class .= ' last';
}

if ($item->deeper) {
    $class .= ' deeper';
    $first_start = true;
}

有可能去掉,例如在顶级的最后一项中添加一个 last ul – 可以替换:

else if( in_array( $item->id, $last_items ) ) {
    $class .= ' last';
}

为:

else if( in_array($item->id, $last_items) && $item->level_diff > 0 ) {
    $class .= ' last';
}

For appointment a last and first classes to the last and first items of branches of the hierarchical menu in current 2.5.4 version of Joomla, I'm add to default.php (it's copied from modules\mod_mainmenu\tmpl\ to templates\mytemplate\html\mod_mainmenu\) this code:

after:

defined('_JEXEC') or die;

insert:

$last_items = array();
foreach( array_reverse( $list, true ) as $v ) {
    if( ! isset( $last_items[$v->parent_id] ) )
        $last_items[$v->parent_id] = $v->id;
}
$first_start = true;

and replace:

if ($item->deeper) {
    $class .= ' deeper';
}

with:

if( $first_start ) {
    $class .= ' first';
    $first_start = false;
}
else if( in_array( $item->id, $last_items ) ) {
    $class .= ' last';
}

if ($item->deeper) {
    $class .= ' deeper';
    $first_start = true;
}

There is the possibility of get rid of such as adding a last class in the last item of the top-level ul – can be replaced:

else if( in_array( $item->id, $last_items ) ) {
    $class .= ' last';
}

with:

else if( in_array($item->id, $last_items) && $item->level_diff > 0 ) {
    $class .= ' last';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文