8

Essentially I'm trying to add multiple sub nav menus and I'm pretty close but I just can't seem to find the right tack to solve my problem ...

I'm using a variation of a Stu Nichols CSS Menu ...

Code explains better ;-)

The relationship required to build this menu would have the top level elements as list-items, child-containers as divs and children and siblings {nth child} as uls ... So the structure would be the following ...

<li><a href='#'>parent</a>
    <div>child-container
    <ul><!--1st Child -->
        <li>item</li>
         ...
    </ul>
    <ul><!--Sibling -->
        <li>item</li>
         ...
    </ul>
    <ul><!--Sibling -->
        <li>item</li>
         ...
    </ul>
    </div> 
</li>

My current line of thought is to add an additional nav_menu_walker to build the sub-menu lists ... However, I'm not sure if I can pass multiple walker's as args to wp_nav_menu ... IE ... ASSERT

    $menu_params = array (

'theme_location'  => 'primary',
'menu'            => 'Main Menu', 
'container'       => 'div', 
'container_class' => 'nav', 
'container_id'    => FALSE,
'menu_class'      => FALSE, 
'menu_id'         => FALSE,
'echo'            => TRUE,
'fallback_cb'     => 'wp_page_menu',
'before'          => FALSE,
'after'           => FALSE,
'link_before'     => FALSE,
'link_after'      => FALSE,
'items_wrap'      => "\n\t" . '<ul>%3$s</ul>' . "\n",
'depth'           => 0,
'walker'          => new Top_Nav_Menu_Walker()
    <!-- Can I Add another Walker Here? -->
);

echo "\t\t<div id=\"navigation\">\n\t\t\t";

wp_nav_menu( $menu_params );

echo "\t\t</div>\n";

}

To obtain the parent -> child-container relationship I've simply added div and ul to the start_lvl function ... Here's where I'm having issues determining how I can create multiple siblings ...

function start_lvl( &$output, $depth = 0, $args = array()) {

    $indent = str_repeat("\t", $depth);     
    $output .=  "\n$indent<div>\n";
    $output .=  "\n$indent<ul>\n";
}  

In order to add classes to the top level anchor elements I have a custom walker. In the start_el function I've injected classes for the anchor items based on the parent class.

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    global $wp_query;
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

    $class_names = $value = '';

    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    $classes[] = 'menu-item-' . $item->ID;

    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

    /* inject some anchor classes */

    if(  $this->$depth == 0) {
        if ( in_array( 'two', $item->classes ) || in_array( 'three', $item->classes )) {
            $item->class = 'oneCol fly';
        }
        if ( in_array( 'four', $item->classes )) {
            $item->class = 'twoCol fly';
        }
    };

    $output .= $indent . '<li' . $id . $value . $class_names .'>';

  $output .= in_array( 'sub_nav_h4', $item->classes ) ? "\n" . $indent . "<h4>\n" : '';

    $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
    $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
    $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
    $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

    /*  This 'should' always be true as WP doesn't support anchor classes    */

    $attributes .= ! empty( $item->class    )       ?   ' class="'  . esc_attr( $item->class            ) . '"' : '';

    $item_output = $args->before;
    $item_output .= '<a' . $attributes .'>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    $item_output .= '</a>';
    $item_output .= $args->after;

    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}

Here's the navigation structure I'm trying to achieve ...

<div id="navigation">
<div class="nav">
        <ul>
            <li class="noCol one"><a href="/">One</a>       </li>
            <li class="threeCol two"><a class="oneCol fly" href="/two/">Two</a> 
                <div>
                <ul>
                        <li><h4><a href="#url">Egestas Sed</a></h4></li>
                        <li><a href="#url">Consequat Leo</a></li>
                        <li><a href="#url">Lacinia &amp; Blandit</a></li>
                        <li><a href="#url">Interdum</a></li>
                        <li><h4><a href="#url">Pellentesque Velit</a></h4></li>
                        <li><a href="#url">Lacinia &amp; Blandit</a></li>
                        <li><a href="#url">Interdum</a></li>
                        <li><a href="#url">Donec non Fringilla</a></li>
                        <li><a href="#url">Fusce Ullamcorper</a></li>
                </ul>
                <ul>
                        <li><h4><a href="#url">Facilisis</a></h4></li>
                        <li><a href="#url">Tristique</a></li>
                        <li><a href="#url">Donec</a></li>
                        <li><h4><a href="#url">Sapien</a></h4></li>
                        <li><a href="#url">Purus</a></li>
                        <li><a href="#url">Congue</a></li>
                        <li><a href="#url">Mattis</a></li>
                </ul>
                <ul>
                        <li><h4><a href="#url">Integer nec Diam</a></h4></li>
                        <li><a href="#url">Morbi Eget Pharetra</a></li>
                        <li><a href="#url">Nulla &amp; Orci</a></li>
                        <li><a href="#url">Eget Sapien Sodales</a></li>
                        <li><h4><a href="#url">Aenean</a></h4></li>
                        <li><a href="#url">Velit Ligula</a></li>
                        <li><a href="#url">Maecenas</a></li>
                </ul>
                </div>
                </li>
                <li class="threeCol three"><a class="oneCol fly" href="/three/">Three</a> 
                <div>
                <ul>
                        <li><h4><a href="#url">Egestas Sed</a></h4></li>
                        <li><a href="#url">Consequat Leo</a></li>
                        <li><h4><a href="#url">Pellentesque Velit</a></h4></li>
                        <li><a href="#url">Lacinia &amp; Blandit</a></li>
                        <li><a href="#url">Interdum</a></li>
                        <li><a href="#url">Donec non Fringilla</a></li>
                        <li><a href="#url">Fusce Ullamcorper</a></li>
                </ul>
                <ul>
                        <li><h4><a href="#url">Facilisis</a></h4></li>
                        <li><a href="#url">Tristique</a></li>
                        <li><a href="#url">Donec</a></li>
                        <li><h4><a href="#url">Sapien</a></h4></li>
                        <li><a href="#url">Purus</a></li>
                        <li><a href="#url">Congue</a></li>
                        <li><a href="#url">Mattis</a></li>
                </ul>
                <ul>
                        <li><h4><a href="#url">Integer nec Diam</a></h4></li>
                        <li><a href="#url">Morbi Eget Pharetra</a></li>
                        <li><a href="#url">Nulla &amp; Orci</a></li>
                        <li><a href="#url">Eget Sapien Sodales</a></li>
                        <li><h4><a href="#url">Aenean</a></h4></li>
                        <li><a href="#url">Velit Ligula</a></li>
                        <li><a href="#url">Maecenas</a></li>
                </ul>
                </div>
                </li>

                <li class="twoCol four"><a class="twoCol fly" href="/four/">Four</a>
                <div>
                <ul>
                        <li><h4><a href="#url">Ut vel Cursus</a></h4></li>
                        <li><a href="#url">Maecenas imperdiet </a></li>
                        <li><a href="#url">Congue Metus</a></li>
                        <li class="fly"><a class="fly" href="#url">Vitae Luctus</a>
                        <ul>
                                <li><a href="#url">Purus Pellentesque</a></li>
                                <li><a href="#url">Pellentesque Sed</a></li>
                                <li><a href="#url">Felis Nunc</a></li>
                        </ul>
                        </li>
                </ul>
                <ul>
                        <li><h4><a href="#url">Morbi Placerat Luctus</a></h4></li>
                        <li><a href="#url">Ut &amp; Eleifend</a></li>
                        <li><a href="#url">Feugiat Euismod</a></li>
                        <li><a href="#url">Tempus Condi</a></li>
                </ul>
                </div>
                </li>
                <li class="noCol five"><a href="/five/">Five</a></li>
                <li class="noCol six"><a href="/six/">Six</a></li>
                <li class="noCol seven"><a href="/seven">Seven</a></li>
        </ul>
</div> <!--</div class="nav"> -->
</div><!--</div id="navigation"> -->
3
  • That structure is imposed by the CSS, right? If so, you need to rethink you CSS code. Learn about pseudo elements, like :nth-child, :before, :after. I highly doubt it that you cannot implement e certain menu design with WP's default menu markup. Can you post a screenshot of how this menu is supposed to look? Sep 30, 2012 at 23:33
  • I tried uploading a screen shot but I don't think I've got the rep here yet :-) fwiw ... this menu works like a champ as a static menu ... NP there ... it's adding multiple unordered lists via a menu walker I'm struggling with ... thanks for your help :-) Oct 1, 2012 at 2:34
  • @EddieB you could always link to the screenshot on an image hosting site Sep 30, 2014 at 3:08

2 Answers 2

1

Frankly, I'd just use a single menu and then apply a CSS class (in the backend) to the navigation element you want the to be.

It's simple enough to add a CSS class in the Menu editing pane, and that solves a lot of your difficulties since you can just check for the presence of that string in your custom walker.

2
  • Thanks Michael ... That's what I'm currently doing and I'm able to get one sub-menu unordered list ... I just can't seem to add multiple lists per menu ... Sep 30, 2012 at 22:32
  • Essentially the issue is being able to add multiple lists for a single child container I.E. parent {li} -> child_container {div} -> child {ul} | sibling {ul} | sibling {ul} Oct 2, 2012 at 14:39
1

You can modify your menu by using walker.

include('subMenu.php');
$menu =  wp_nav_menu( array('menu' => 'YOUR-MENU-NAME','menu_class' => 'megamenu','walker' => new subMenu));

create a file subMenu.php in theme folder add below code.

<?php
class subMenu extends Walker_Nav_Menu {
    function end_el(&$output, $item, $depth=0, $args=array()) {

    if( 'parent' == $item->title ){
        $output .= '<div> //child-container
        <ul>
                    <li><h4><a href="#url">Egestas Sed</a></h4></li>
                    <li><a href="#url">Consequat Leo</a></li>
                    <li><a href="#url">Lacinia &amp; Blandit</a></li>
                    <li><a href="#url">Interdum</a></li>
                    <li><h4><a href="#url">Pellentesque Velit</a></h4></li>
                    <li><a href="#url">Lacinia &amp; Blandit</a></li>
                    <li><a href="#url">Interdum</a></li>
                    <li><a href="#url">Donec non Fringilla</a></li>
                    <li><a href="#url">Fusce Ullamcorper</a></li>
            </ul>
            <ul>
                    <li><h4><a href="#url">Facilisis</a></h4></li>
                    <li><a href="#url">Tristique</a></li>
                    <li><a href="#url">Donec</a></li>
                    <li><h4><a href="#url">Sapien</a></h4></li>
                    <li><a href="#url">Purus</a></li>
                    <li><a href="#url">Congue</a></li>
                    <li><a href="#url">Mattis</a></li>
            </ul>
            <ul>
                    <li><h4><a href="#url">Integer nec Diam</a></h4></li>
                    <li><a href="#url">Morbi Eget Pharetra</a></li>
                    <li><a href="#url">Nulla &amp; Orci</a></li>
                    <li><a href="#url">Eget Sapien Sodales</a></li>
                    <li><h4><a href="#url">Aenean</a></h4></li>
                    <li><a href="#url">Velit Ligula</a></li>
                    <li><a href="#url">Maecenas</a></li>
            </ul>
        </div>
        ';
    }
    $output .= "</li>\n";  
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.