douou5933 2013-08-05 12:56
浏览 50
已采纳

Joomla菜单拆分PHP

I am currently trying to override a joomla menu so that it looks like:

item | item | item | item LOGO item | item | item |item

So basically i am trying to split the menu in half into two <ul>'s I got is working but unfortunately i magically got it broken...

Here's the unedited code for the menu:

<?php
/**
 * @package     Joomla.Site
 * @subpackage  mod_menu
 *
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

// Note. It is important to remove spaces between elements.
?>
<?php // The menu class is deprecated. Use nav instead. ?>
<ul class="nav menu<?php echo $class_sfx;?>"<?php
    $tag = '';
    if ($params->get('tag_id') != null)
    {
        $tag = $params->get('tag_id').'';
        echo ' id="'.$tag.'"';
    }
?>>
<?php
foreach ($list as $i => &$item) :
    $class = 'item-'.$item->id;
    if ($item->id == $active_id)
    {
        $class .= ' current';
    }

    if (in_array($item->id, $path))
    {
        $class .= ' active';
    }
    elseif ($item->type == 'alias')
    {
        $aliasToId = $item->params->get('aliasoptions');
        if (count($path) > 0 && $aliasToId == $path[count($path) - 1])
        {
            $class .= ' active';
        }
        elseif (in_array($aliasToId, $path))
        {
            $class .= ' alias-parent-active';
        }
    }

    if ($item->type == 'separator')
    {
        $class .= ' divider';
    }

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

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

    if (!empty($class))
    {
        $class = ' class="'.trim($class) .'"';
    }

    echo '<li'.$class.'>';

    // Render the menu item.
    switch ($item->type) :
        case 'separator':
        case 'url':
        case 'component':
        case 'heading':
            require JModuleHelper::getLayoutPath('mod_menu', 'default_'.$item->type);
            break;

        default:
            require JModuleHelper::getLayoutPath('mod_menu', 'default_url');
            break;
    endswitch;

    // The next item is deeper.
    if ($item->deeper)
    {
        echo '<ul class="nav-child unstyled small">';
    }
    // The next item is shallower.
    elseif ($item->shallower)
    {
        echo '</li>';
        echo str_repeat('</ul></li>', $item->level_diff);
    }
    // The next item is on the same level.
    else {
        echo '</li>';
    }
endforeach;
?></ul>

And Here's the Code I edited and got it working until now:

<?php
/**
 * @package     Joomla.Site
 * @subpackage  mod_menu
 *
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

// Note. It is important to remove spaces between elements.
?>
<?php // The menu class is deprecated. Use nav instead. ?>
<?php if($class_sfx == " menu-logo"): ?>
<ul class="nav menu<?php echo $class_sfx;?> span4 pull-left menu-left"<?php
    $tag = '';
    if ($params->get('tag_id') != null)
    {
        $tag = $params->get('tag_id').'';
        echo ' id="'.$tag.'"';
    }
endif;
?>
<?php if($class_sfx != " menu-logo"): ?>
<ul class="nav menu<?php echo $class_sfx;?>"<?php
    $tag = '';
    if ($params->get('tag_id') != null)
    {
        $tag = $params->get('tag_id').'';
        echo ' id="'.$tag.'"';
    }
endif;
?>>
<?php 
$counter = 0;
foreach ($list as $i => &$item) :
    $class = 'item-'.$item->id;
     if($item->params->get('menu-anchor_css') == "dropdown-toggle") {
    $class = 'dropdown item-'.$item->id;
    } else {
    $class = 'item-'.$item->id;
    }

    if (in_array($item->id, $path))
    {
        $class .= ' active';
    }
    elseif ($item->type == 'alias')
    {
        $aliasToId = $item->params->get('aliasoptions');
        if (count($path) > 0 && $aliasToId == $path[count($path) - 1])
        {
            $class .= ' active';
        }
        elseif (in_array($aliasToId, $path))
        {
            $class .= ' alias-parent-active';
        }
    }

    if ($item->type == 'separator')
    {
        $class .= ' divider';
    }

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

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

    if (!empty($class))
    {
        $class = ' class="'.trim($class) .'"';
    }

    echo '<li'.$class.'>';

    // Render the menu item.
    switch ($item->type) :
        case 'separator':
        case 'url':
        case 'component':
        case 'heading':
            require JModuleHelper::getLayoutPath('mod_menu', 'default_'.$item->type);
            break;

        default:
            require JModuleHelper::getLayoutPath('mod_menu', 'default_url');
            break;
    endswitch;

    // The next item is deeper.
    if ($item->deeper)
    {
        echo '<ul class="nav-child dropdown-menu">';
    }
    // The next item is shallower.
    elseif ($item->shallower)
    {
        echo '</li>';
        echo str_repeat('</ul></li>', $item->level_diff);
    }
    // The next item is on the same level.
    else {
        $counter++;
        echo '</li>';
        if($counter == 4 && $class_sfx == " menu-logo"){
            echo "</ul>";
            echo "<div class='logo span4 hidden-phone'><img src='".JURI::base( true )."logo.png' /></div>";
            echo "<ul class='nav menu menu-right pull-right'>";
        }
    }
endforeach;
?></ul>
<div class='clearfix'></div>

Sorry I know its ALOT of code, but cant really leave things out... any help greatly appreciated.

Please not i need to use a solution based on the solution i came up with.. I have just done way to much CSS on the method i provided to revert right now...

  • 写回答

2条回答 默认 最新

  • dsa88886666 2013-08-05 14:16
    关注

    Use $item->level instead of counting everything with $counter only count items that have a level lower then 2. See code below:

    <?php
    /**
     * @package     Joomla.Site
     * @subpackage  mod_menu
     *
     * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
     * @license     GNU General Public License version 2 or later; see LICENSE.txt
     */
    
    defined('_JEXEC') or die;
    
    // Note. It is important to remove spaces between elements.
    ?>
    <?php // The menu class is deprecated. Use nav instead. ?>
    <?php if($class_sfx == " menu-logo"): ?>
    <ul class="nav menu<?php echo $class_sfx;?> span4 pull-left menu-left"<?php
        $tag = '';
        if ($params->get('tag_id') != null)
        {
            $tag = $params->get('tag_id').'';
            echo ' id="'.$tag.'"';
        }
    endif;
    ?>
    <?php if($class_sfx != " menu-logo"): ?>
    <ul class="nav menu<?php echo $class_sfx;?>"<?php
        $tag = '';
        if ($params->get('tag_id') != null)
        {
            $tag = $params->get('tag_id').'';
            echo ' id="'.$tag.'"';
        }
    endif;
    ?>>
    <?php 
    foreach ($list as $i => &$item) :
        if($item->level < 2):
            $counter += count($item);
        endif;
    
        $class = 'item-'.$item->id;
         if($item->params->get('menu-anchor_css') == "dropdown-toggle") {
        $class = 'dropdown item-'.$item->id;
        } else {
        $class = 'item-'.$item->id;
        }
    
        if (in_array($item->id, $path))
        {
            $class .= ' active';
        }
        elseif ($item->type == 'alias')
        {
            $aliasToId = $item->params->get('aliasoptions');
            if (count($path) > 0 && $aliasToId == $path[count($path) - 1])
            {
                $class .= ' active';
            }
            elseif (in_array($aliasToId, $path))
            {
                $class .= ' alias-parent-active';
            }
        }
    
        if ($item->type == 'separator')
        {
            $class .= ' divider';
        }
    
        if ($item->deeper)
        {
            $class .= 'dropdown-menu deeper';
        }
    
        if ($item->parent)
        {
            $class .= ' parent dropdown';
        }
    
        if (!empty($class))
        {
            $class = ' class="'.trim($class) .'"';
        }
    
        echo '<li'.$class.'>';
    
        // Render the menu item.
        switch ($item->type) :
            case 'separator':
            case 'url':
            case 'component':
            case 'heading':
                require JModuleHelper::getLayoutPath('mod_menu', 'default_'.$item->type);
                break;
    
            default:
                require JModuleHelper::getLayoutPath('mod_menu', 'default_url');
                break;
        endswitch;
    
        // The next item is deeper.
        if ($item->deeper)
        {
            echo '<ul class="nav-child dropdown-menu">';
        }
        // The next item is shallower.
        elseif ($item->shallower)
        {
            echo '</li>';
            echo str_repeat('</ul></li>', $item->level_diff);
        }
        // The next item is on the same level.
        else {
            echo '</li>';
        }
        if($counter == 4 && $class_sfx == " menu-logo"){
                echo "</ul>";
                echo "<div class='logo span4 hidden-phone'><img src='".JURI::base( true )."logo.png' /></div>";
                echo "<ul class='nav menu menu-right pull-right'>";
            }
    endforeach;
    ?></ul>
    <div class='clearfix'></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题