2011-03-04 27 views
2

高亮活动菜单项,我在重写的Joomla 1.5 mod_mainmenu模块,但无法获得.active#current CSS类或ID显示在页面上。它显示出下面的HTML菜单:中的Joomla

<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应用到活动元素,但似乎没有办法做到这一点。有什么想法吗?

谢谢。

UPDATE:这里是我创建了该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); 
?> 
+0

你可以张贴在覆盖文件中的代码?活动菜单ID和类在核心模板文件中设置,如果您覆盖它,则需要对此进行说明。 –

+0

谢谢,布伦特。明天我会发布代码;现在无法访问它。 – Alex

+0

菜单结构好像不是由mod_mainmenu生成的。再次检查,这可能是由其他菜单模块生成的。 – Prakash

回答

3

试试这个问题,以下是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); 
?> 
+0

谢谢,普拉卡什。这工作!但为什么?我的mod_mainmenu覆盖有什么问题? – Alex

+0

Do'h !!我看到了我的方式错误:我将mod函数拼写为'modMainMenuXMLCallback',然后通过调用'modMyMainMenuXMLCallback'来渲染它。荡。 – Alex

1

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

+0

我正在使用没有template.css文件的自定义模板。 – Alex