0

I am using.

<?php wp_nav_menu($args); ?>

to get menu from WordPress.

It returns the menus with <ul> and <li> structure.
How can i set class name for every <li> tag uniquely.

  • there's a filter you can use to do this: nav_menu_css_class – onetrickpony Jun 23 '11 at 10:57
1

You can use the CSS class for the menu.
Under "Screen Options", flag "CSS Classes", under "Show Advanced Properties".

Then, for every menu item, you have an additional input that is named "CSS Classes"

| improve this answer | |
1

I think the best solution would be to write your own walker class for it and pass it as an argument, but I must admit that's not too straight forward if you are not too familiar with wordpress development.

I would solve it like this:

$menu_items = wp_get_nav_menu_items('your_menu_slug_or_id');        
$class = '';
$count = count($menu_items);
$i = 0;

foreach ($menu_items as $menu_item) {
    $class = 'item-'.(++$i);
?>
    <li class="<?php echo $class?>">
        <!-- add menu title and url here as you please -->
    </li>
<?php
}
| improve this answer | |
0

This should help you buddy - http://wpveda.com/add-custom-classes-wpnavmenu-condition/

| improve this answer | |
0

What is the reason for needing custom classes?

WordPress menus include a number of classes on the <li> elements when the function is run and should be everything you need to work with.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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