More control over wp_nav_menu()
• ~200 words • 1 minute read
I can't count how many times I've Googled some random error or function and found the answer I needed on a personal blog for someone I've never heard of. Because of that, I try to make a point to document fixes to tiny, nagging problems as I discover them.
In that vein, here's a tip for using wp_nav_menu()
in your WordPress theme: You may want to have portions of your menu dynamic and controlled by WordPress' built-in menu system, but also include static elements at the bottom of the menu — maybe something like a login link that changes depending on whether or not the user is already logged-in or a members-only area. By default calling wp_nav_menu()
renders something like this:
That's nice and convenient, but you can't hard-code any additional <li>
elements to this list in your theme. What you can do though is render the list items without the encapsulating <ul>
element like this:
wp_nav_menu( "items_wrap" => '%3$s', "container" => "");
This means you'll have to add the encapsulating <ul>
element theme. The final product would probably look like this:
- '%3$s', "container" => ""); ?>
- Members Login Members Only Area
Nice little trick that makes the built-in menu management a little more flexible.