我使用此代码在菜单栏中添加了一个搜索字段;
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__) )
)
?>
答案 0 :(得分:2)
使用add_filter
时,必须告诉WordPress使用$ args变量提供回调函数。简而言之,试试这个:
function menu_search($items, $args){
if( $args->theme_location == 'footer-nav-mid') {
$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;
}
add_filter('wp_nav_menu_items','menu_search', 10, 2); // <-- the 10 is the default priority, the 2 is the number of variables to pass to the callback function.
将“ footer-nav-mid ”替换为菜单位置的slug。当您使用register_nav_menu()
注册菜单时设置此项:
register_nav_menu('footer-nav-mid', 'Middle Footer Navigation');
希望这有帮助!
答案 1 :(得分:1)
您可以将第二个参数($ args)传递给wp_nav_menu_items过滤器,以检查theme_location值是否正确。
function menu_search($items, $args){
if( $args->theme_location == 'YOUR THEME LOCATION VALUE' ){
$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', 10, 2);
希望它有所帮助!
答案 2 :(得分:0)
这是一个更简单的解决方案 - 将以下内容添加到functions.php文件中:
add_filter('wp_nav_menu_items','add_search_box_to_menu', 10, 2);
function add_search_box_to_menu( $items, $args ) {
if( $args->theme_location == 'primary' )
return $items."<li class='menu-header-search'>".get_search_form(false)."</li>";
return $items;
}
在此函数中,您将看到IF语句正在检查所需位置 - 具体而言,此示例正在检查主菜单:
if( $args->theme_location == 'primary' )
你也可以用这样的东西来确定哪个菜单:
$args->menu->slug == ‘the_menu_slug’
如果有主菜单,则返回&#34;返回$ items&#34;引用:
return $items."<li class='menu-header-search'>".get_search_form(false)."</li>";
在这种情况下,我创建了一个列表项并连接了 get_search_form() WP功能。
最重要的是,要将此菜单作为列表项显示在菜单中,您需要将get_search_form()设置为false:
get_search_form(false)
这是因为get_search_form()函数中的follow参数:
$回波 (bool)(可选)默认为echo而不返回表单。 默认值:true
这将成功地在任何特定菜单中实现所需的搜索表单。