WPTrick : Add new element into wp menu
by ronggur on January 9, 2011
A simple trick to add new element into your wp menu.In this case I want to add custom search form to may
Just add this codes to your wp code I usually put in in functions.php
<?php
// Filter wp_nav_menu() to add additional links and other output
function custom_nav_menu_items($items, $args) {
$search = '<li id="search">';
$search .= custom_searchform();
$search .= '</li>';
$items = $items .$search;
return $items;
}
add_filter( 'wp_nav_menu_items', 'custom_nav_menu_items', 10, 2 );
function custom_searchform(){
$search = get_search_query();
if($search) $search_text = $search;
else $search_text = "Search";
$form = '<form method="get" id="searchform" action="'.get_bloginfo('home').'">';
$form .= '<fieldset>';
$form .= '<input type="text" value="'.$search_text.'" name="s" id="s" />';
$form .= '<input type="hidden" id="searchsubmit" />';
$form .= '</fieldset>';
$form .= '</form>';
return $form;
}
?>
Method above will add search form to all wp menu. So how about if I only want to add to specific menu location?
Here we goes
<?php
# apply only ini 'header-menu' location
if($args->theme_location == 'header-menu'){
$search = '<li id="search">';
$search .= rh_searchform();
$search .= '</li>';
$items = $items .$search;
}
?>
Finish,
-rmh-
-
http://www.athanassiadis.se Georgios
-
marc