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.

Have this function :

class my_walker extends Walker_Nav_Menu {

        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[] = 'menu-item-' . $item->ID;

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

            $id = apply_filters('nav_menu_item_id', 'menu-item-' . $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);
        }

    }

I need to add my own counter instead of item->ID to add $myCounter(that will increment).

This :

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

To this :

$id = apply_filters('nav_menu_item_id', 'menu-item-' . $myCounter, $item, $args);

I want to adjust to crazy css menu.How can I do this?

Thanks in advance.

share|improve this question
    
Hi @user1147 - Can you please change the title of this post to describe what the question is about? The answers on the site will benefit so many more people if they can see from the question titles if the question is same as the issue they are having, or not. Based on your current title I don't really have a clue what this question is about and don't have the time right now to read through the code to figure it out. Thanks in advance. –  MikeSchinkel Oct 10 '10 at 1:12
    
Sorry MikeSchinkel,I change the title,sorry on my bad english. –  user1147 Oct 10 '10 at 3:34
    
@user1147 - Thank you, and apologize, I didn't realize you were ESL. –  MikeSchinkel Oct 12 '10 at 8:03

1 Answer 1

up vote 1 down vote accepted

Do you need to modify walker or hook into it unmodified?

Since there is filter provided for that id, it is easy to hook into. Try this:

add_filter( 'nav_menu_item_id', 'modify_menu_item_id' );

function modify_menu_item_id( $text ) {

    static $myCounter;

    if( !isset( $myCounter ) )
         $myCounter = 0;

    return 'menu-item-' . ++$myCounter;
}

Update Changing walker

 // add to start of start_el method
        static $myCounter;
        if( !isset( $myCounter ) )
            $myCounter = 0;

and

// change id lines to this
        $id = $args->depth ? 'menu-item-' . $item->ID : 'menu-item-' . ++$myCounter;
        $id = apply_filters('nav_menu_item_id', $id, $item, $args);
        $id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';

I tried to exclude sub-menus by checking for current depth and I think it should work properly with multiple menus because each will get new instance of walker object.

share|improve this answer
    
I need also to modify walker,need to put submenus out of menus ul.Will try your code.Thanks. –  user1147 Oct 8 '10 at 9:43
    
I there a way to do this,but to modify walker? –  user1147 Oct 8 '10 at 9:47
    
Do you need one global counter or have it reset for every submenu? Latter seems tricky... –  Rarst Oct 8 '10 at 9:49
    
do not need for submenus,just for top menu,and need to put submenu out of top menus ul.finally beginning to understand how this works,thank you. –  user1147 Oct 8 '10 at 10:03
    
I had updated answer with how it might work for walker. Please test properly in your setup before use. –  Rarst Oct 8 '10 at 10:25

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.