【问题标题】:Wordpress search field in specific menu bar特定菜单栏中的 Wordpress 搜索字段
【发布时间】:2018-06-19 17:04:02
【问题描述】:

我使用此代码在我的菜单栏中添加了一个搜索字段;

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__) ) 
) 


 ?>

【问题讨论】:

标签: php wordpress themes


【解决方案1】:

当您使用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');

希望这会有所帮助!

【讨论】:

  • 啊啊!相信我,我没有复制你的答案! :)
  • @iEmanuele 我相信你!您仍然忘记更新 add_filter 调用以将 2 个变量发送到回调函数;)干杯!
  • 我感到绝望,请查看this。 :)
  • 短时间内信息量太大……我想这只是睡个好觉,我回来了。
  • 不幸的是,我在使用它时收到此错误消息; “注意:未定义的变量:搜索.....”它说问题出在“返回 $items”。 $搜索;'线。奇怪的是这个错误出现在我的所有其他菜单中,除了我在代码中命名的那个。
【解决方案2】:

您可以将第二个参数 ( $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);

希望对你有帮助!

【讨论】:

  • 如果不调整 add_filter 呼叫,您的解决方案将无法工作 - 请参阅我的答案。
  • @usernotfound,谢谢。我猜默认参数编号是 2 而不是 1。:)
  • @iEmanuele 不客气 ;) 默认为 1:codex.wordpress.org/Function_Reference/add_filter
【解决方案3】:

这是一个更简单的解决方案 - 将以下内容添加到您的 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() 函数中的跟随参数:

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

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

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多