2016-08-22 1 views
2

Wordpressでは、どのようにwp_nav_menuを使ってすべてのサブメニューliにボタンやdivを追加できますか?Wordpressでサブメニューの折り返しを変更

これは私の現在のコードです:

<?php wp_nav_menu(array(
    'theme_location' => 'main_menu', 
    'items_wrap'=>'%3$s', 
    'container' => false 
)); ?> 

これは私の所望の出力です:だから

<li class="submenu"> 
    <a>Link 1</a> 
    <ul> 
    <li><a>Link 2</a></li> 
    </ul> 
    <button type="button">Click Me!</button> 
</li> 
+0

をあなたはおそらく、あなた自身の[メニューウォーカークラス](HTTPSを作成する必要があります。 //www.smashingmagazine.com/2015/10/customize-tree-like-data-structures-wordpress-walker-class/)。あなたが求めていることは、実際には些細なことではありません。 –

答えて

1

あなたがそれらを理解するまで、Custom Walkersは、で動作するように痛みのビットです。

以下のカスタムウォーカーコードでは、必要なものを得ることができます。あなたのテーマのfunctions.phpファイルにこれを追加します。

class Custom_Button_Walker extends Walker_Nav_Menu { 
    // We only care about the "end level" part of the menu, where closing </ul> tags are generated 
    public function end_lvl(&$output, $depth = 0, $args = array()) { 
     // This is from WP core code 
     $indent = str_repeat("\t", $depth); 
     // This line ensures we only add it on the proper level 
     $button = (0 == $depth) ? "{$indent}<button type=\"button\">Click Me!</button>\n" : ''; 
     // This line is modified to include the button markup 
     $output .= "{$indent}</ul>\n{$button}"; 
    } 
} 

は、カスタム歩行器を使用するには、そのようなあなたのwp_nav_menuの呼び出しを変更します。

wp_nav_menu(array(
    'theme_location' => 'main_menu', 
    'items_wrap'  =>'%3$s', 
    'container'  => FALSE, 
    'walker'   => new Custom_Button_Walker() 
)); 
+0

うわー、ありがとう。私はTYPO3に慣れていますが、カスタムメニューはかなり簡単です。 1つの質問:それをレベル1とレベル2に適用するにはどうすればよいですか?私は3階層メニューを持っています... – user500665

+1

すべての深度に適用したい場合は、 '$ button =" {$ indent} \ n ";' –

関連する問題