• Resolved infopursuits

    (@infopursuits)


    I am trying to add a search box to my top bar menu.

    The problem I’m having is that the search box is appearing on both the top bar menu and the main menu. I only want it to appear on the top menu.

    I used the following code in the functions.php file (please see note below about the section of the code I have put in bold):

    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();
     <strong>if( $args->theme_location == ‘nav-top’ )</strong>
            $items .= '
    
    <li>' . $searchform . '</li>
    ';
    
        return $items;
    }

    Here is a pastebin link if that doesn’t render properly here: https://pastebin.com/2QEWQc2k

    The section in bold is where I’m supposed to be able to name the menu where I want the search box to appear. Whether I used frontier-menu-top, frontier-menu-primary, top-menu, or main-menu, the search box persists in appearing in both areas. If I use something arbitrary, it takes the search box off of both.

    Is there any way to include it in one and exclude it from the other? As I mentioned, I only want it on the top menu.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Theme Author ronangelo

    (@ronangelo)

    Always wrap your codes in backticks or code tags before posting here so it doesn’t mess things up like it did. The link to the pastebin would have sufficed.

    Anyway, I don’t really know why your code’s not working but If I were to add a search box on the top menu, this is how I would do it:

    function add_top_search_box() {
    	return get_search_form();
    }
    add_action( 'frontier_after_top_bar', 'add_top_search_box' );

    You’re using a child theme, right?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    What Ron said. The post was ‘fixed’ but the forums may have eaten some of your content :/

    Thread Starter infopursuits

    (@infopursuits)

    Yes, I’m using a child theme, and I’m sorry about the code posting. I am still learning and I don’t know the proper procedure for posting code. I don’t even know what “back ticks” means. I think I understand what code tags are though. I’ll try that next time, or I will just use the pastebin link.

    Thank you for your assistance. I replaced the code I was using with the code you are using and it worked like a charm.

    One question, though, and if there’s not an easy answer, I will be fine with the way things stand now. Is there a way to move the search bar over a little? Perhaps using a style in CSS? Not a big deal, if not.

    Again, thank you so much for your wonderful support. I couldn’t ask for a better theme or better support.

    Here’s a link to my site: https://www.behindeverypro.com

    Theme Author ronangelo

    (@ronangelo)

    Check either of these codes.

    #top-bar .search-form {
        float: left;
        padding-left: 60px;
    }
    #nav-top {
        float: left;
        padding-left: 60px;
    }
    #top-bar .search-form {
        float: right;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trouble adding search box to top menu’ is closed to new replies.