[wp-hackers] Adding class to first menu item of submenu in wp_nav_menu

Otto otto at ottodestruct.com
Mon Jun 28 00:17:14 UTC 2010


On Sun, Jun 27, 2010 at 5:28 PM, Gavin Pearce <Gavin.Pearce at 3seven9.com> wrote:
> Evening all,
>
> Just wondering if any of you kind ladies and gentleman has found a way to add a "first" class, to the first item of each submenu within a menu generated by wp_nav_menu.
>
> Adding a class to the first ever li is easy (preg_replace on menu-item x1), adding it to the first li "under" each and every submenu UL has confused me, without going into stupidly complex preg_replaces.
>
> E.g.   (look for the FIRST's) ::
>
> <ul>
>        <li class="menu-item">Home</li>
>        <li class="menu-item">Page1
>                <ul class="submenu">
>                        <li class="FIRST menu-item">Subpage 1</li>
>                        <li class="menu-item">Subpage 2</li>
>                </ul>
>        </li>
>        <li class="menu-item">Page3</li>
>        <li class="menu-item">Page4
>                <ul class="submenu">
>                        <li class="FIRST menu-item">Subpage 1</li>
>                        <li class="menu-item">Subpage 2</li>
>                </ul>
>        </li>
> </ul>


Why would you need that? Just use "ul.sub-menu li:first-child". CSS2
has you covered on that score.

-Otto


More information about the wp-hackers mailing list