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.

I have menu like this:

<ul>
        <li>
            <div class="buttons-4" title="some_title">
                <a href="[some_url]">[something]</a>
            </div>
        </li>
        <li>
            <div class="buttons-5" title="some_title">
                <a href="[some_url]">[something]</a>
            </div>
        </li>
    </ul>

and I've try this:

$args = array(
    'theme_location' => 'my-theme-location',
    'menu' => '',
    'container' => '',
    'container_id' => '',
    'menu_class' => '',
    'menu_id' => '',
    'echo' => true,
    'before' => '<div class="buttons-%1$s" title="some_title">',
    'after' => '</div>',
    'link_before' => '',
    'link_after' => ''
);

wp_nav_menu( $args );

I need every element inside menu item can have class [or ID is also okay] dynamicly like class="buttons-5" and other class="button-6" an so on just like my example. How can I get dynamicly my buttons class? Sorry for my bad english.

share|improve this question
    
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. –  engelen Jun 18 at 8:16
    
thanks @engelen, sorry for my unclear question. Little bit better for now? –  user1841035 Jun 18 at 8:27

2 Answers 2

up vote 0 down vote accepted

Try this:

function custom_walker_nav_menu_start_el($item_output, $item, $depth, $args){
  $output = '<div class="buttons-'.$item->ID.'" title="some_title">';
  $output .= $item_output;
  $output .= '</div>';
  return $output;
}
add_filter( 'walker_nav_menu_start_el', 'custom_walker_nav_menu_start_el' , 10, 4 );

This uses walker_nav_menu_start_el filter to add custom content before and after the link item. ID is appended to buttons-. If you need this customization for only specific menu then you can check $args as your requirement.

share|improve this answer

Thanks @nilambar. It works!. This is my last code:

 $menu_middle_left = array(
            'theme_location' => 'middle-left',
            'menu' => '',
            'container' => '',
            'container_class' => '',
            'container_id' => '',
            'menu_class' => '',
            'menu_id' => '',
            'echo' => true,
            'fallback_cb' => 'wp_page_menu',
            'link_before' => '',
            'link_after' => '',
            'walker' => new Walker_Nav_Menu()
        );

        wp_nav_menu($menu_middle_left);

I put your code in my functions.php:

function custom_walker_nav_menu_start_el($item_output, $item, $depth, $args){
  $output = '<div class="buttons-'.$item->ID.'" title="some_title">';
  $output .= $item_output;
  $output .= '</div>';
  return $output;
}
add_filter( 'walker_nav_menu_start_el', 'custom_walker_nav_menu_start_el' , 10, 4 );
share|improve this answer

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.