突出显示 Joomla 中的活动菜单项 [英] Highlight active menu item in Joomla

查看:23
本文介绍了突出显示 Joomla 中的活动菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Joomla 1.5 中覆盖了 mod_mainmenu 模块,但无法让 .active#current CSS 类或 ID 显示在页面上.它显示了菜单的以下 HTML:

I'm overriding the mod_mainmenu module in Joomla 1.5 and am unable to get the .active or #current CSS class or ID to show on the page. It's showing the following HTML for the menu:

<ul id="top-nav" class="flatList">
  <li access="0" level="1" id="1">
    <a href="#">
      <span class="embed embed-top-nav">Home</span>
      <p>news, highlights</p>
    </a>
  </li>
  <li access="0" level="1" id="4">
    <a href="/content/index.php?option=com_content&amp;view=article&amp;id=1&amp;Itemid=4">
      <span class="embed embed-top-nav">Watch UNC-TV</span>
      <p>schedule, programs</p>
    </a>
  </li>
</ul>

我已经读到 mod_mainmenu 会自动将 activecurrent 插入其中的某处,这样您就可以知道哪个项目是当前活动的菜单选择.但是我在生成的 HTML 中没有看到任何一个.我想对活动元素应用一些 CSS,但似乎没有任何方法可以做到这一点.有什么想法吗?

I've read that the mod_mainmenu will automatically insert either active or current somewhere into this so you can tell which item is the currently active menu selection. But I'm not seeing either of those in the generated HTML. I'd like to apply some CSS to the active element, but there doesn't seem to be any way to do this. Any thoughts?

谢谢.

更新:这是我创建的 mod_mainmenu 的代码:

UPDATE: Here's the code of the mod_mainmenu I've created:

<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

if ( ! defined('fancyMenuPatch') ) 
{
  function fancyMenuPatch($result,$tag){
    $menu   = JSite::getMenu();
    $active = $menu->getActive();

    // Add to the start of the UL tag.
    $begin_ul = "<ul id="top-nav" class="flatList">";
    $begin_span = "<span class="embed embed-top-nav">";

    $home_p = "Home</span><p>news, highlights</p></a>";
    $watch_p = "Watch UNC-TV</span><p>schedule, programs</p></a>";
    $learn_p = "Learn</span><p>education, unc-tv kids</p></a>";
    $support_p = "Support Us</span><p>pledge, volunteer, corporate</p></a>";
    $contact_p = "Contact</span><p>feedback, connect, share</p></a>";

    // do the replacements
    $result = str_replace("<ul class="menu">",$begin_ul, $result);
    $result = str_replace("<span>", $begin_span, $result);
    $result = str_replace("Home</span></a>",$home_p,$result);
    $result = str_replace("Watch UNC-TV</span></a>",$watch_p,$result);
    $result = str_replace("Learn</span></a>",$learn_p,$result);
    $result = str_replace("Support Us</span></a>",$support_p,$result);
    $result = str_replace("Contact</span></a>",$contact_p,$result);

    return $result;
  }
  define('fancyMenuPatch', true);
}

if ( ! defined('modMainMenuXMLCallbackDefined') )
{
function modMainMenuXMLCallback(&$node, $args)
{
  $user = &JFactory::getUser();
  $menu = &JSite::getMenu();
  $active   = $menu->getActive();
  $path = isset($active) ? array_reverse($active->tree) : null;

  if (($args['end']) && ($node->attributes('level') >= $args['end']))
  {
    $children = $node->children();
    foreach ($node->children() as $child)
    {
      if ($child->name() == 'ul') {
        $node->removeChild($child);
      }
    }
  }

  if ($node->name() == 'ul') {
    foreach ($node->children() as $child)
    {
      if ($child->attributes('access') > $user->get('aid', 0)) {
        $node->removeChild($child);
      }
    }
  }

  if (($node->name() == 'li') && isset($node->ul)) {
    $node->addAttribute('class', 'parent');
  }

  if (isset($path) && (in_array($node->attributes('id'), $path) || in_array($node->attributes('rel'), $path)))
  {
    if ($node->attributes('class')) {
      $node->addAttribute('class', $node->attributes('class').' active');
    } else {
      $node->addAttribute('class', 'active');
    }
  }
  else
  {
    if (isset($args['children']) && !$args['children'])
    {
      $children = $node->children();
      foreach ($node->children() as $child)
      {
        if ($child->name() == 'ul') {
          $node->removeChild($child);
        }
      }
    }
  }

  if (($node->name() == 'li') && ($id = $node->attributes('id'))) {
    if ($node->attributes('class')) {
      $node->addAttribute('class', $node->attributes('class').' item'.$id);
    } else {
      $node->addAttribute('class', 'item'.$id);
    }
  }

  if (isset($path) && $node->attributes('id') == $path[0]) {
    $node->addAttribute('id', 'current');
  } else {
    $node->removeAttribute('id');
  }
  $node->removeAttribute('rel');
  $node->removeAttribute('level');
  $node->removeAttribute('access');
}
  define('modMainMenuXMLCallbackDefined', true);
}
ob_start();

modMainMenuHelper::render($params, 'modMyMainMenuXMLCallback');
$menu_html = ob_get_contents();
ob_end_clean(); 

if($params->get('menutype')=="mainmenu"){
  $tag = $params->get('tag_id');
}

//output the menu!
echo fancyMenuPatch($menu_html,$tag);
?>

推荐答案

试试这个,这里是 mod_mainmenu 的代码(覆盖):

Try this, here's code for mod_mainmenu (override):

<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

if ( ! defined('fancyMenuPatch') ) 
{
  function fancyMenuPatch($result,$tag){
    $menu   = JSite::getMenu();
    $active = $menu->getActive();

    // Add to the start of the UL tag.
    $begin_ul = "<ul id="top-nav" class="flatList">";
    $begin_span = "<span class="embed embed-top-nav">";

    $home_p = "Home</span><p>news, highlights</p></a>";
    $watch_p = "Watch UNC-TV</span><p>schedule, programs</p></a>";
    $learn_p = "Learn</span><p>education, unc-tv kids</p></a>";
    $support_p = "Support Us</span><p>pledge, volunteer, corporate</p></a>";
    $contact_p = "Contact</span><p>feedback, connect, share</p></a>";

    // do the replacements
    $result = str_replace("<ul class="menu">",$begin_ul, $result);
    $result = str_replace("<span>", $begin_span, $result);
    $result = str_replace("Home</span></a>",$home_p,$result);
    $result = str_replace("Watch UNC-TV</span></a>",$watch_p,$result);
    $result = str_replace("Learn</span></a>",$learn_p,$result);
    $result = str_replace("Support Us</span></a>",$support_p,$result);
    $result = str_replace("Contact</span></a>",$contact_p,$result);

    return $result;
  }
  define('fancyMenuPatch', true);
}

if ( ! defined('modMyMainMenuXMLCallbackDefined') )

  {

  function modMyMainMenuXMLCallback(&$node, $args)

  {

  $user  = &JFactory::getUser();

  $menu  = &JSite::getMenu();

  $active  = $menu->getActive();

  $path  = isset($active) ? array_reverse($active->tree) : null; if (($args['end']) && ($node->attributes('level') >= $args['end']))
  {

  $children = $node->children();

  foreach ($node->children() as $child)

  {

  if ($child->name() == 'ul') {

  $node->removeChild($child);

  }

  }

  }

 if ($node->name() == 'ul') {

  foreach ($node->children() as $child)

  {

  if ($child->attributes('access') > $user->get('aid', 0)) {

  $node->removeChild($child);

  }

  }

  }

 if (($node->name() == 'li') && isset($node->ul)) {

  $node->addAttribute('class', 'parent');

  }

 if (isset($path) && in_array($node->attributes('id'), $path))

  {

  if ($node->attributes('class')) {

  $node->addAttribute('class', $node->attributes('class').' active');

  } else {

  $node->addAttribute('class', 'active');

  }

  }

  else

  {

  if (isset($args['children']) && !$args['children'])

  {

  $children = $node->children();

  foreach ($node->children() as $child)

  {

  if ($child->name() == 'ul') {

  $node->removeChild($child);

  }

  }

  }

  }

 if (($node->name() == 'li') && ($id = $node->attributes('id'))) {

  if ($node->attributes('class')) {

  $node->addAttribute('class', $node->attributes('class').' item'.$id);

  } else {

  $node->addAttribute('class', 'item'.$id);

  }

  }

 if (isset($path) && $node->attributes('id') == $path[0]) {

  $node->addAttribute('id', 'current');

  } else {

  $node->removeAttribute('id');

  }

  $node->removeAttribute('level');

  $node->removeAttribute('access');



  }

  define('modMyMainMenuXMLCallbackDefined', true);

}

ob_start();

modMainMenuHelper::render($params, 'modMyMainMenuXMLCallback');
$menu_html = ob_get_contents();
ob_end_clean(); 

if($params->get('menutype')=="mainmenu"){
  $tag = $params->get('tag_id');
}

//output the menu!
echo fancyMenuPatch($menu_html,$tag);
?>

这篇关于突出显示 Joomla 中的活动菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆