Monday 12 May 2014

Wordpress: How to add a search bar to the main menu

Add the following code into function.php file:
add_filter('wp_nav_menu_items','add_search_box', 10, 2);
function add_search_box($items, $args) {
    if($args->theme_location == 'header-menu') {
        ob_start();
        get_search_form();
        $searchform = ob_get_contents();
        ob_end_clean();
 
        return $items .= '<li id="searchform-item">' . $searchform . '</li>';
    }
    return $items;
}

add the following in the style.css file:
#searchform{
    margin:7px;
}

#searchform-item input[type="text"] {
    width: auto;
}
#searchform-item {
    float: right;
}

No comments:

Post a Comment