SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search Box in Main Nav bar (WordPress)

    Hi
    I am building a WP site. It has multiple menus. I want a search box to appear in the main (primary) menu only.
    I am using this code:
    PHP Code:
    <?php add_filter('wp_nav_menu_items','add_search_box'102);
     
      
    function 
    add_search_box($items$args) {
     
            
    ob_start();
            
    get_search_form();
            
    $searchform ob_get_contents();
            
    ob_end_clean();
     
        
    $items .= '<li class="searchbox">' $searchform '</li>';
      return 
    $items;   
     
        
    }
    ?>
    but it is putting a searchbox into my main menu and sidebar menus.

    Anyone know a way to target the main menu only?


    Thanks

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    I found this site, which may help you.
    http://www.wpbeginner.com/wp-themes/...rdpress-menus/

    It states to use
    PHP Code:
    $args->theme_location == 'primary' 
    So you would end up with
    PHP Code:
    <?php add_filter('wp_nav_menu_items','add_search_box'102); 
      
       
    function 
    add_search_box($items$args) { 
      if (
    $args->theme_location == 'primary') {
            
    ob_start(); 
            
    get_search_form(); 
            
    $searchform ob_get_contents(); 
            
    ob_end_clean(); 
      
        
    $items .= '<li class="searchbox">' $searchform '</li>'
      }
      return 
    $items;    
           
    }
    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I tried that but the search box disappeared altogether. I had actually tried adding that line before but assumed I must have done it wrong, so just copied and pasted yours, but no luck.

    This:

    <?php wp_nav_menu( array('menu' => 'Project Nav' )); ?>

    seems to be the code for targeting a specific menu, but I'm not really a PHP expert and can't work out where to put it!

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Can you change it to this and give me the additional output it produces?
    PHP Code:
    <?php add_filter('wp_nav_menu_items','add_search_box'102); 
      
       
    function 
    add_search_box($items$args) { 
      
    var_dump($args); // I want the output of this, as that should help identify what we are doing wrong :)
      
    if ($args->theme_location == 'primary') {
            
    ob_start(); 
            
    get_search_form(); 
            
    $searchform ob_get_contents(); 
            
    ob_end_clean(); 
      
        
    $items .= '<li class="searchbox">' $searchform '</li>'
      }
      return 
    $items;    
           
    }
    ?>

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, here you go:
    Code:
    object(stdClass)#274 (16) { ["menu"]=> string(0) "" ["container"]=> bool(false) ["container_class"]=> string(0) "" ["container_id"]=> string(0) "" ["menu_class"]=> string(4) "menu" ["menu_id"]=> string(3) "nav" ["echo"]=> bool(true) ["fallback_cb"]=> string(12) "wp_page_menu" ["before"]=> string(0) "" ["after"]=> string(0) "" ["link_before"]=> string(0) "" ["link_after"]=> string(0) "" ["items_wrap"]=> string(36) "
    
        %3$s
    
    " ["depth"]=> int(3) ["walker"]=> string(0) "" ["theme_location"]=> string(15) "main_navigation" }
    Thanks for your help

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Okay, try this code
    PHP Code:
    <?php add_filter('wp_nav_menu_items','add_search_box'102); 
      
       
    function 
    add_search_box($items$args) { 
      if (
    $args->theme_location == 'main_navigation') {
            
    ob_start(); 
            
    get_search_form(); 
            
    $searchform ob_get_contents(); 
            
    ob_end_clean(); 
      
        
    $items .= '<li class="searchbox">' $searchform '</li>'
      }
      return 
    $items;    
           
    }
    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic - works perfectly. Thank you so much.
    So 'main_navigation' instead of 'primary'

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Yes, to clarify a bit, and explain why I used main_navigation, the output of $args showed the theme_location as 'main_navigation' not 'primary', so I just updated primary to be main_navigation

    Glad it worked.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •