2

I've created a simple menu in wp-admin>appearance>menus called main-nav. Works fine.

However, I'd like to add a custom element to the end of the menu... a search box like the search box in the apple.com menu bar. I can't work out where the menus get constructed in code. where can I can add this... any ideas?

(I'm using the starkers theme)

EDIT

Thanks to tnorthcutt and hakre for pointing me in the right direction. The solution was to put this code in with the other 'add_filter' stuff in my theme's functions.php

add_filter('wp_nav_menu_items','search_box_function');
function search_box_function ($nav){
return $nav."<li class='menu-header-search'><form action='http://example.com/' id='searchform' method='get'><input type='text' name='s' id='s' placeholder='Search'></form></li>";
}

UPDATE

@tnorthcutt's solution is great for when you have just one menu on your screen, but if you add second menu it appends the search box to that menu too. How would you target just one menu? i've registered my menu's like so:

register_nav_menus( array(
  'primary' => __( 'Primary Navigation', 'twentyten' ),
  'secondary'=>__('Secondary Menu', 'twentyten' ),

 ) );

..and the secondary one gets shown like this:

wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'secondary' ) ); 
2
  • 1
    @cannyboy - since you've already marked the question as accepted, please put your "update" in a new question ... otherwise it probably won't get answered.
    – EAMann
    Sep 23, 2010 at 13:22
  • ok, i've done that
    – cannyboy
    Sep 23, 2010 at 14:14

1 Answer 1

1

Try this:

add_filter('wp_nav_menu_items','search_box_function');
function search_box_function {
search box code goes here
}

For reference, check out Bill Erickson's excellent tutorial on accomplishing this with the Genesis framework.

6
  • Thanks... where would that code go?
    – cannyboy
    Sep 7, 2010 at 19:18
  • 1
    @cannyboy please try in your themes function.php.
    – hakre
    Sep 7, 2010 at 21:47
  • this is great for just one menu, but how would i target specific menus? See my updated question.
    – cannyboy
    Sep 23, 2010 at 12:18
  • There is version of that hook for targeting specific menus, it is called like wp_nav_menu_{$menu->slug}_items. Replace {$menu->slug} with slug of menu you want to target.
    – Rarst
    Sep 23, 2010 at 12:23
  • d'you mean this should go in the add_filter? when I change it to add_filter('wp_nav_menu_primary_items','search_box_function'); I get no result.
    – cannyboy
    Sep 23, 2010 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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