Php 特定菜单栏中的Wordpress搜索字段
我使用此代码在菜单栏中添加了一个搜索字段Php 特定菜单栏中的Wordpress搜索字段,php,wordpress,themes,Php,Wordpress,Themes,我使用此代码在菜单栏中添加了一个搜索字段 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="
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');
功能菜单搜索($items){
$search='';
$search.='';
$search.='';
$search.='';
$search.='';
$search.=' ';
返回$items.$search;
}
添加过滤器(“wp\U导航菜单项”、“菜单搜索”);
但这会在我所有的菜单中添加一个栏,因为我使用了多个菜单,这是一个问题。如何使搜索栏只显示在主导航栏中
我试着使用这里的教程;但搜索栏将不再显示在我的菜单栏中。可能我使用的名称或主题位置错误,这就是我的菜单屏幕“默认(主菜单)”中显示的内容
如果需要,这是我的函数文件的其余部分
<?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__) )
)
?>
使用
添加过滤器时,必须告诉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.
希望这有帮助 使用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.
希望这有帮助 您可以将第二个参数($args)传递给wp\u nav\u menu\u items过滤器,以检查主题位置值是否正确
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);
功能菜单搜索($items,$args){
如果($args->theme\u location=='您的主题位置值'){
$search='';
$search.='';
$search.='';
$search.='';
$search.='';
$search.=' ';
}
返回$items.$search;
}
添加过滤器(“wp\U导航菜单项”,“菜单搜索”,10,2);
希望有帮助 您可以将第二个参数($args)传递给wp\u nav\u menu\u items过滤器,以检查主题位置值是否正确
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);
功能菜单搜索($items,$args){
如果($args->theme\u location=='您的主题位置值'){
$search='';
$search.='';
$search.='';
$search.='';
$search.='';
$search.=' ';
}
返回$items.$search;
}
添加过滤器(“wp\U导航菜单项”,“菜单搜索”,10,2);
希望有帮助 这是一个简单得多的解决方案-将以下内容添加到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;
}
您也可以使用类似的方法来确定哪个菜单:
$args->menu->slug == ‘the_menu_slug’
如果有主菜单,它将返回“return$items”引号中的所有内容:
这是因为get_search_form()函数中有以下参数:
$echo
(bool)(可选)默认为回显而不返回表单。
默认值:true
- ()
这将在任何特定菜单中成功实现所需的搜索表单。这是一个更简单的解决方案-将以下内容添加到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;
}
您也可以使用类似的方法来确定哪个菜单:
$args->menu->slug == ‘the_menu_slug’
如果有主菜单,它将返回“return$items”引号中的所有内容:
这是因为get_search_form()函数中有以下参数:
$echo
(bool)(可选)默认为回显而不返回表单。
默认值:true
- ()
这将成功地在任何特定菜单中实现所需的搜索表单。Ah!相信我,我没有抄你的答案!:)@我相信你!您仍然忘记更新add_filter
调用以向回调函数发送2个变量;)干杯我觉得没希望了,查查。:)在短时间内获取的信息太多。。。我想这只是一个很好的晚上的睡眠,我回来时是新的。不幸的是,我在使用它时收到了这个错误消息;'注意:未定义变量:search in…’它说问题出在‘return$items’上$搜索线路。奇怪的是,这个错误出现在我的所有其他菜单中,除了我在代码中命名的菜单。啊!相信我,我没有抄你的答案!:)@我相信你!您仍然忘记更新add_filter
调用以向回调函数发送2个变量;)干杯我觉得没希望了,查查。:)在短时间内获取的信息太多。。。我想这只是一个很好的晚上的睡眠,我回来时是新的。不幸的是,我在使用它时收到了这个错误消息;'注意:未定义变量:search in…’它说问题出在‘return$items’上$搜索线路。奇怪的是,这个错误出现在我的所有其他菜单中,除了我在代码中命名的菜单。如果不调整add\u filter
call,您的解决方案将无法工作–请参阅我的答案。@usernotfound,谢谢。我猜默认参数编号是2而不是1::)@不客气;)默认值为1:如果不调整add_filter
call,您的解决方案将无法工作–请参阅我的答案。@usernotfound,谢谢。我猜默认参数编号是2而不是1::)@不客气;)默认值为1:@usernotfound已首先应答。:)@iEmanuele不在乎谁先回答,但您的解决方案目前不起作用,请参见我的评论;)@usernotfound已首先回答。:)@iEmanuele不在乎谁先回答,但您的解决方案目前不起作用,请参见我的评论;)