简体   繁体   English

突出显示Joomla中的活动菜单项

[英]Highlight active menu item in Joomla

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. 我在Joomla 1.5中覆盖了mod_mainmenu模块,无法获取.active#current CSS类或ID来显示在页面上。 It's showing the following HTML for the menu: 该菜单显示以下HTML:

<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>

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. 我读过mod_mainmenu会自动在其中插入activecurrent这样您就可以知道哪个项目是当前活动菜单选择。 But I'm not seeing either of those in the generated HTML. 但是我在生成的HTML中没有看到任何一个。 I'd like to apply some CSS to the active element, but there doesn't seem to be any way to do this. 我想将一些CSS应用于active元素,但是似乎没有任何方法可以做到这一点。 Any thoughts? 有什么想法吗?

Thanks. 谢谢。

UPDATE : Here's the code of the mod_mainmenu I've created: 更新 :这是我创建的mod_mainmenu的代码:

<?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);
?>

Try this, here's code for mod_mainmenu (override): 试试看,这是mod_mainmenu(覆盖)的代码:

<?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);
?>

检查您的模板文件夹,有一个template.css文件,您可以在其中找到!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM