is that possible, without javascript hacks?
like this:
<ul class="my_menu">
<li class="first"> ... </li>
<li> ... </li>
<li> ... </li>
<li class"with_sub"> ...
<ul class="my_menu_sub">
<li class="first"> ... </li>
<li> ... </li>
<li> ... </li>
<li class="last"> ... </li>
</ul>
</li>
<li> ... </li>
<li> ... </li>
<li class="last"> ... </li>
</ul>
A better and simpler approach:
Here is rough snippet that takes care of modifying menu output and adding first/last to first and last class (outer
ul
is not applied at this stage so doesn’t count). Note – requires PHP5 forstrripos()
I am bit stuck with how to make it handle nested lists, but it should get you started at least.
Here is a function for only adding the first/last classes to the parent menu items. For most CSS styling, this is all that is necessary.
Learn more about the new Menus API in wordpress 3. You can give any element it’s own class manually. Also, once mastered, it makes menus a thing of joy to edit.
If you have nested menus
I like the simplicity of Ismaelj’s answer, but there needs to be more if you want sub-menu classes.
If you don’t need support for IE8 or lower, don’t forget that you can also use pure CSS:
jQuery browser support is even better, but it sounds like you’re trying to avoid that.
Here is some better code for adding first and last menu item classes that includes support for nested submenus.
You can find the gist here and the associated tutorial here.
How about:
and maybe some http://selectivizr.com/
Pure CSS, works for me. This will work with submenus also