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');

但这会将栏添加到我的所有菜单中,因为我正在使用多个菜单,这是一个问题。我怎样才能使搜索栏只显示在我的主导航栏中?

我尝试使用此处找到的教程;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__) ) 
) 


 ?>
4

3 回答 3

3

使用 时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 ”替换为您的菜单位置。这是在您使用以下方式注册菜单时设置的register_nav_menu()

register_nav_menu('footer-nav-mid', 'Middle Footer Navigation');

希望这可以帮助!

于 2013-09-08T20:08:16.437 回答
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);

希望能帮助到你!

于 2013-09-08T20:12:09.767 回答
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’

如果有主菜单,它会返回“return $items”引号中的任何内容:

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() 函数中有以下参数:

$echo (bool) (可选) 默认回显并且不返回表单。默认值:真

这将在任何特定菜单中成功实现所需的搜索表单。

于 2016-04-21T18:10:31.867 回答