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.

How can I get an item counter working in a custom walker ?

I need the corresponding number (not the id) of each menu item for some class names, because my elements have position:absolute and each one has its class (class_name_1, class_name_2, ...) with individual top margins. I'm not using CSS3, so I can't use the nth-child pseudo class.

Thanks in advance !

EDIT: I managed to achieve this using (modified from example on codex):

if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) 
{
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    $menu_items = wp_get_nav_menu_items($menu->term_id);

    $menu_list = '<div id="side_menu">';
    $current_item = 1;
    foreach ( (array) $menu_items as $key => $menu_item ) 
    {
        $title = $menu_item->title;
        $url = $menu_item->url;
        $menu_list .= '<a class="tab_'.$current_item.'" href="'.$url.'"><span>'.$title.'</span></a>';
        $current_item += 1;
    }
    $menu_list .= '</div>';
} 
else 
{
    $menu_list = '';
}

Anyway, a solution to do this with the walker may be useful in the future. Thanks !

share|improve this question

closed as too localized by toscho Jul 13 '12 at 19:55

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Would be helpful to see the code you need the counter in. –  t31os Aug 5 '11 at 10:40
    
I was trying to modify (simplify) this example wpcodesnippets.info/blog/lets-put-icons-in-our-menus.html . However, in the mean time, I used the example from the wordpress codex on wp_get_nav_menu_items(). –  Mio Aug 5 '11 at 11:19
1  
How does the above code relate to the link you've provided, sorry i don't follow... (can you not simply do as you've done above inside the custom walker?) ... –  t31os Aug 5 '11 at 11:37
    
They are related in the way that the same thing can be achieved in two different ways. My goal was to render the items in a menu with custom markup. The specific need was for a counter. So, yes, I can and I have done as above. As I said in the edit, answer for my initial question remains useful for future reference. –  Mio Aug 5 '11 at 12:38

Browse other questions tagged or ask your own question.