wp-bootstrap-navwalker icon indicating copy to clipboard operation
wp-bootstrap-navwalker copied to clipboard

How to add a search menu to my navigation.

Open Markj89 opened this issue 12 years ago • 14 comments

I'm having some trouble implementing a search function into the navigation. I put in the form under the <php navbar stuff. But the search function ends up on the left side of the navigation, how can I fix this?

www.charter-motorcoach.com

Markj89 avatar Jul 17 '14 22:07 Markj89

Markj89, One should create a custom search form with Bootstrap markup/css. Applying the classes as seen in http://getbootstrap.com/components/#navbar-forms. This will place the search form to the right side of the navbar. However, to place it within the walker one could either use jquery to append the form within the ul or modify the fall back to include the search form. (modifying the fallback is theory; not tested)

LouisMilotte avatar Jul 18 '14 00:07 LouisMilotte

How would I append it to the navbar?

$('form').append( $( 'navbar-form' ) ); ??

Markj89 avatar Jul 18 '14 19:07 Markj89

The jQuery method can become memory intensive. However, if this is the route you wish to take versus modifying the fallback method:

You would append using the ID of the navigation bar. For instance, assuming that the ID is menu-menu-1 you would type:

jQuery('#menu-menu-1').append('<li><form>..</form></li>');

You would need to apply the attributes and innerhtml that you believe your form should have.

ADVICE: It is strongly suggested in interest to mobile devices that you limit the amount of javascript running. By this, one should modify the fallback method to include the search form. This is of course assuming you did not want to place the form to the right of the navigation bar.

LouisMilotte avatar Jul 18 '14 19:07 LouisMilotte

I wouldn't know how to modify the fallback method, haha.

Markj89 avatar Jul 18 '14 19:07 Markj89

Here's what I have so far.

http://jsfiddle.net/GreendayXv6/N3Y4r/

Markj89 avatar Jul 18 '14 19:07 Markj89

Very wrong,

In wordpress you have to use jQuery not $ when calling jQuery functions. Wordpress does not support the use of $.

Chaining jQuery functions the way you did does not work, call the functions individually.

Do not use input type search, by bootstrap documentation the search button should be a button element.

Input search does not have a src attribute.

Please review http://getbootstrap.com/components/#navbar for instructions on creating a navbar-form

As for modifying the class library to include this form, my mistake; it isn't in the fallback. If you still want the jQuery method stick with it. However, if you want instructions on how to modify the library let me know and i will put it together once I get the chance.

LouisMilotte avatar Jul 18 '14 20:07 LouisMilotte

Yea the instructions for modifying the the library would help. Sorry, this is my first time ever using WordPress.

Markj89 avatar Jul 18 '14 20:07 Markj89

I tried taking a look at what they did to put this together. Unfortunately, with my limited understanding of Wordpress Walker classes I do not know how to modify the code to include the search bar. Please use the jQuery provided here: http://jsfiddle.net/6rrJ7/

As a note to the creators, if an option can be given to include the search form or proper instruction be given, please do so.

LouisMilotte avatar Jul 22 '14 23:07 LouisMilotte

Oh It's ok, I made a variable and then added "get_search_form() to it. It placed it on the right side, but now I have to figure out why the search function doesn't work. Whenever I input something and click, it just refreshes the page haha.

Markj89 avatar Jul 23 '14 14:07 Markj89

Could you share? If you don't mind supplying the code that you used to do this. Also, I might be able to help troubleshoot why it isn't searching.

LouisMilotte avatar Jul 23 '14 20:07 LouisMilotte

Work this js code: http://jsfiddle.net/kuaza/6z6bsh30/

KilicSelcuk avatar Aug 17 '14 12:08 KilicSelcuk

Is there a reason you're doing this with JS instead of PHP? You can do this with PHP using something like this:

        <div class="collapse navbar-collapse navbar-ex1-collapse">
            <?php wp_nav_menu( array(
                'theme_location' => 'top',
                'depth'          => 2,
                'container'      => false,
                'menu_class'     => 'nav navbar-nav',
                'walker'         => new wp_bootstrap_navwalker(),
                'fallback_cb'    => 'wp_bootstrap_navwalker::fallback'
                )
            ); ?>
                <form id="searchform" class="navbar-form navbar-right" role="search" action="<?php echo esc_url( site_url() ); ?>" method="get">
                    <div class="form-group">
                        <input id="s" name="s" type="text" class="form-control" placeholder="<?php esc_attr_e( 'Search &hellip;', 'alienship' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>">
                    </div>
                    <button id="searchsubmit" type="submit" name="submit" class="btn btn-default">Submit</button>
                </form>
        </div>

mindctrl avatar Aug 17 '14 13:08 mindctrl

@mindctrl this good, its work, thank you :)

KilicSelcuk avatar Aug 17 '14 17:08 KilicSelcuk

You could also do 'search' => get_search_form(); after 'wp_bootstrap_navwalker::fallback' Make sure it's in the array.

Markj89 avatar Apr 08 '16 17:04 Markj89