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.

Is there a way I can give each wordpress children (subcategory UL lists) unique ID's? I'm creating a dropdown list for subcategories and I want to position them but :nth-child isn't supported by everything yet.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

as far as I know, all menu items already have unique ID´s ("menu-item-XX"), may they be first or second level. the UL itself has a different class : menu for first level and sub-menu for second. hence , in css for example .menu li and .sub-menu li will give you the differentiation for styling. you could also do that without the unique id or classes , by simply targeting the different levels of the menu, .menu UL LI for first level .menu UL LI UL LI for second Anotehr alternative is to use javascript or jQuery.. But I think this is not what you want here ..

EDIT I I forgot to mention that one can also add classes in the menu editor itself on item base , and also the has class arguments..Not to mention the fact that you can create 2 custom-menus wih your name*

EDIT II If you really want a custom function (everyone these days wants one..) than this one will add a class to each menu item which is equal to the menu title.

add_filter('nav_menu_css_class', 'dl_menu_class_by_title', 10, 2 );
function dl_menu_class_by_title($classes, $item) {
        $classes[] = $item->title;
    return $classes;
}

.. But I still stand by my first answer that in 98% of the cases - it is an unnecessary overkill ..

Edit III - since you are unable to implement none of the above ways to solution (all valid) - Here is The jQuery way :

 <script>
$(function(){
    var i =0;
 $('ul.sub-menu').addClass (function(idx) {

     return "item-" + idx;
 });
 });
 </script>
share|improve this answer
    
Well I'm trying to think as dynamically as possible here. I think if the admin changes the subcategories won't the menu-item-xx change also? –  Howdy_McGee Feb 1 '12 at 0:15
    
yes, the menu-item-xx ID will change, but not the classes.. this should be enough for simple styling. if you want to do something more elaborated, please describe it ... (and also read my edit) –  krembo99 Feb 1 '12 at 0:17
    
Well I'm talking about the UL's. not each seperate list item. I want to give .children ID's of the parent category or something similar. That way I can move each list item exactly where I want them. –  Howdy_McGee Feb 1 '12 at 1:23
1  
The UL already has a class of it´s own . and you could also change it via <?php wp_nav_menu( $args ); ?> (one argument is the UL ID and other is CLASS) . Could you explain what you want to achieve that is not achievable by simple CSS and classes (assuming you have a basic knowladge of CSS)? I do not fully understand what is the problem of moving the items with the default behaviour of wordpress Nav_menu, like 99 % of the websites are doing. If you care to post your code and problem, Maybe I could help better.. –  krembo99 Feb 1 '12 at 3:24
    
I think your over thinking it. I just want unique IDs on each child UL so then I can move each UL separately instead of together (like I would have to do with a class) but I'm also keeping it all dynamic so I don't want to use the menu-item-xx (plus its a class which is not what I want). Right now I have the navigation pulling in dynamically via wp_list_categories(). I was looking in the admin nav files to see if I could add an argument and in incremeneter that would auto add an id of id=blah1 etc. And really the only code I have is wp_list_categories() and css. –  Howdy_McGee Feb 1 '12 at 5:05
show 4 more comments

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.