Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My theme has three 3 menus: two that are dropdown menus and one that is different. I want to remove the div, class, etc. from the menu that is different.

For Example: I have three menus

  1. topnav ( drop down )
  2. mainmenu ( drop down)
  3. mymenu ( normal )

My current code:

<div id="megaMenu" class="megaMenuContainer megaMenu-nojs megaFullWidth megaMenuHorizontal megaMenuOnHover wpmega-autoAlign wpmega-withjs wpmega-noconflict"><ul id="megaUber" class="megaMenu"><li id="menu-item-987" class="menu-item menu-item-type-post_type menu-item-object-post current-menu-item ss-nav-menu-item-0 ss-nav-menu-item-depth-0 ss-nav-menu-reg"><a href="http://localhost/wordpress/?p=967"><span class="wpmega-link-title">who we are</span></a></li>
<li id="menu-item-986" class="menu-item menu-item-type-post_type menu-item-object-post ss-nav-menu-item-1 ss-nav-menu-item-depth-0 ss-nav-menu-reg"><a href="http://localhost/wordpress/?p=969"><span class="wpmega-link-title">management team</span></a></li>
<li id="menu-item-985" class="menu-item menu-item-type-post_type menu-item-object-post ss-nav-menu-item-2 ss-nav-menu-item-depth-0 ss-nav-menu-reg"><a href="http://localhost/wordpress/?p=971"><span class="wpmega-link-title">award</span></a></li>
<li id="menu-item-984" class="menu-item menu-item-type-post_type menu-item-object-post ss-nav-menu-item-3 ss-nav-menu-item-depth-0 ss-nav-menu-reg"><a href="http://localhost/wordpress/?p=973"><span class="wpmega-link-title">career</span></a></li>
<li id="menu-item-983" class="menu-item menu-item-type-post_type menu-item-object-post ss-nav-menu-item-4 ss-nav-menu-item-depth-0 ss-nav-menu-reg"><a href="http://localhost/wordpress/?p=975"><span class="wpmega-link-title">contact us</span></a></li>
</ul></div>

I want to remove everything from mymenu, just want to want <li>menu name </li>. Additionally, I want active class applied to activated menu item as <li class="active">menu name </li>.

share|improve this question

1 Answer 1

You can use another walker than WordPress pre-build one.

Add this to your functions.php file in the theme:

class my_cleaner_walker extends Walker {
    var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
    var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
    function start_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"sub-menu\">\n";
    }
    function end_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }
    function start_el(&$output, $item, $depth, $args) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
        $class_names = $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes = in_array( 'active', $classes ) ? array( 'active' ) : array();
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = strlen( trim( $class_names ) ) > 0 ? ' class="' . esc_attr( $class_names ) . '"' : '';
        $id = apply_filters( 'nav_menu_item_id', '', $item, $args );
        $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
        $output .= $indent . '<li' . $id . $value . $class_names .'>';
        $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        ) .'"' : '';
        $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 );
    }
    function end_el(&$output, $item, $depth) {
        $output .= "</li>\n";
    }
}

Then make sure to specify the new custom Walker when calling wp_nav_menu().

<?php
  $walker = new Clean_Nav_Walker();
  $attr = array( 'theme_location' => 'quicklink', 'walker' => $walker);

  wp_nav_menu( $attr );
?>
share|improve this answer
    
thanks man.. thanks alot... i tested its bit work.. but actually as i am new in wp. i can't figure out how to pull my menu.. now my shortcode is <?php wp_nav_menu( array('menu' => 'quicklink' )); ?> so where i put quicklink in your code.. i tried but can't successfully pull menu yet... anyway.. thanks a ton again for your great help. –  pagol Jun 16 '12 at 8:38
    
Se my updated code how to print your menu. –  Pontus Abrahamsson Jul 3 '12 at 20:48
1  
here have another solution too stackoverflow.com/questions/5222140/… –  pagol Aug 6 '12 at 4:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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