我使用此代码在菜单栏中添加了一个搜索字段;
function menu_search($items){
$search = '<li class="menusearch">';
$search .= '<form method="get" id="searchform" action="/">';
$search .= '<input type="text" class="field" name="s" id="s" placeholder="Search" />';
$search .= '<input type="submit" class="menusubmit" name="submit" id="searchsubmit" value="Search" />';
$search .= '</form>';
$search .= '</li>';
return $items . $search;
}
add_filter('wp_nav_menu_items','menu_search');
但这会将栏添加到我的所有菜单中,因为我正在使用多个菜单,这是一个问题。我怎样才能使搜索栏只显示在我的主导航栏中?
我尝试使用此处找到的教程;http://www.wpbeginner.com/wp-themes/how-to-add-custom-items-to-specific-wordpress-menus/但搜索栏将不再出现在我的菜单栏中。也许我使用了错误的名称或主题位置,这就是我的菜单屏幕“默认(主菜单)”中显示的内容。
如果需要,这是我的函数文件的其余部分;
<?php
ob_start();
if ( function_exists( 'add_image_size' ) ) { add_image_size( 'orbit-custom', 920, 300 ); }
/**
* Add a search bar to the navigation menu.
*
* @since Twenty Twelve 1.0
*/
function menu_search($items){
$search = '<li class="menusearch">';
$search .= '<form method="get" id="searchform" action="/">';
$search .= '<input type="text" class="field" name="s" id="s" placeholder="Search" />';
$search .= '<input type="submit" class="menusubmit" name="submit" id="searchsubmit" value="Search" />';
$search .= '</form>';
$search .= '</li>';
return $items . $search;
}
add_filter('wp_nav_menu_items','menu_search');
// This adds more than one menu location
add_action( 'init', 'register_multiple_menus' );
function register_multiple_menus() {
register_nav_menus(
array(
'footer-nav-mid' => 'Middle Footer Navigation',
'footer-nav-left' => 'Left Footer Navigation',
'footer-nav-right' => 'Right Footer Navigation'
)
);
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Downloads Button Homepage',
'before_widget' => '<div id="%1$s" class="widget %2$s buttons">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Locator Button Homepage',
'before_widget' => '<div id="%1$s" class="widget %2$s buttons">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Specials Button Homepage',
'before_widget' => '<div id="%1$s" class="widget %2$s buttons">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Store Locator',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Email Me',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Download Left',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Download Mid',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'Download Right',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
if (
!isset( $_POST['custom_meta_box_nonce'] )
|| !wp_verify_nonce( $_POST['custom_meta_box_nonce'], basename(__FILE__) )
)
?>