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

1

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

それが役に立てば幸い!

于 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) (オプション) デフォルトはエコーし、フォームを返しません。デフォルト値: true

これにより、特定のメニューで目的の検索フォームが正常に実現されます。

于 2016-04-21T18:10:31.867 に答える