WordPress.org

Ready to get started?Download WordPress

Forums

Need to specify a menu (2 posts)

  1. madmedic10
    Member
    Posted 1 year ago #

    Ive added a search bar to my navigation menu by placing this in my functions.php:

    add_filter('wp_nav_menu_items','add_search_box', 10, 2);
    function add_search_box($items, $args) {
    
            ob_start();
            get_search_form();
            $searchform = ob_get_contents();
            ob_end_clean();
    
            $items .= '
    
    <li>' . $searchform . '</li>
    ';
    
        return $items;
    }

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum's parser.]

    The problem is that i have two menus on my site, and the search box shows up on both menus. Is there a way to specify which menu? Ive searched google and most of this forum with no luck. Thanks.

    My site is
    GearMoose

    i've deleted the code for now until i can find a solution.

  2. bcworkz
    Member
    Posted 1 year ago #

    Your code needs a way to differentiate which page it's on somehow. Then you can wrap the search box code in an IF block that checks for this feature. Perhaps something passed in the arguments for the wp_nav_menu() call? Or one of the Conditional Tags?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.