サポート » 使い方全般 » wp_nav_menu()で子メニュー、孫メニューをdivで囲むには

  • WordPress初心者です。
    下記のようなことを考えています。
    いろんな記事を読みながら試しているのですが、2 ~ 3がうまくいかず、debugで警告が表示されます。
    ご教示いただけますと幸いです。

    1. メガメニューで3階層まであるメニューを表示させたい
    2. クリックで2階層以降を展開させるために、各階層をdivで囲みたい
    3. 各階層ごとにdivとliにクラスを分けたい
                <?php
                    wp_nav_menu ( $megamenu = array (
                        'theme_location'  => 'global',
                        'menu'            => 'global',
                        'menu_class'      => 'nav_global-parents',
                        'menu_id'         => '',
                        'container'       => 'nav',
                        'container_class' => 'nav_global-container',
                        'container_id'    => '',
                        'fallback_cb'     => 'wp_page_menu',
                        'before'          => '',
                        'after'           => '',
                        'link_before'     => '',
                        'link_after'      => '',
                        'echo'            => true,
                        'depth'           => 0,
                        'walker'          => new Walker_Nav_Menu(),
                        'theme_location'  => '',
                        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
                    ));
                ?>
    

    debugで表示された警告

    Warning: Declaration of megamenu::start_lvl(&$output, $depth) should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = NULL) in /var/www/html/wp-content/themes/autoFun/functions.php on line 17
    
    Warning: Declaration of megamenu::start_el(&$output, $item, $depth, $args = Array, $id = 0) should be compatible with Walker_Nav_Menu::start_el(&$output, $item, $depth = 0, $args = NULL, $id = 0) in /var/www/html/wp-content/themes/autoFun/functions.php on line 42
    
    
    class megamenu extends Walker_Nav_Menu {
      function start_lvl( &$output, $depth ) {
    
          // 階層の深さに応じてインデント(前方に空白)する。
          $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
          $display_depth = ( $depth + 1);
          $classes = array(
              'nav_global-children',
              ( $display_depth >=3 ? 'nav_global-grantChildren' : '' ),
              );
          $class_names = implode( ' ', $classes );
          $classes_container = array(
              'nav_global-children-container', // とりあえず全部に sub-menu クラスをつける
              ( $display_depth >=3 ? 'nav_global-grantChildren-container' : '' ),
              );
          $class_names_container = implode( ' ', $classes_container );
    
          // 組み立て
          $output .= '\n' . $indent . '<div class="'. $class_names_container . '\n' . '>' .'<ul class="' . $class_names . '">' . '\n';
      }
    
      // ここからliタグの設定 start_el() で開始タグ。end_el() で終了タグを指定できる。el = element
       function start_el( &$output, $item, $depth, $args=array(), $id=0 ) {
          global $wp_query;
          $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // さっきと一緒
    
          // depth に応じてクラス付け替え
          $depth_classes = array(
              ( $depth == 0 ? 'nav_global-parent' : 'nav_global-child' ),
              ( $depth >=2 ? 'nav_global-child' : 'nav_global-grantChild' ),
              ( $depth >= 3 ? 'nav_global-grantChild' : '' ),
          );
          $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );
    
          // passed classes
          $classes = empty( $item->classes ) ? array() : (array) $item->classes;
          $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
    
          $output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';
    
          // 各属性指定
          $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
          $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
          $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
          $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
          $attributes .= ' class="menu-link ' . ( $depth > 0 ? 'nav_global-child-link' : 'nav_global-parent-link' ) . '"';
        
          $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
              $args->before,
              $attributes,
              $args->link_before,
              apply_filters( 'the_title', $item->title, $item->ID ),
              $args->link_after,
              $args->after
          );
        
          // 組み立て
          $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
      }
      }
    

    表示させたいHTML

    
    <nav class="nav_global-container">
        <ul class="nav_global-parents">
            <li class="nav_global-parent is_toggle">
                親メニュー
                <div class="nav_global-children-container">
                    <ul class="nav_global-children">
                        <li class="nav_global-child is_opened">
                            子メニュー
                            <div class="nav_global-grantChildren-container is_active">
                                <ul class="nav_global-grantChildren">
                                    <li class="nav_global-grantChild">
                                        孫メニュー
                                    </li>
                                </ul>
                        </li>
            </li>
        </ul>
    
    
    • このトピックはbrasshが2年、 7ヶ月前に変更しました。
    • このトピックはbrasshが2年、 7ヶ月前に変更しました。
    • このトピックはbrasshが2年、 7ヶ月前に変更しました。
    • このトピックはbrasshが2年、 7ヶ月前に変更しました。
3件の返信を表示中 - 1 - 3件目 (全3件中)
  • @brassh さん

    はじめまして。
    警告エラーの通り、start_lvlstart_el の引数に問題があるように見えます。

    参考:WordPress で PHP7.0にアップデートしたら start_lvl() と start_el() でエラーが出た場合の対処法

    この対応で2と3の問題が解消されるかは分かりませんが、一度試してみていただけますでしょうか。

    トピック投稿者 brassh

    (@brassh)

    @wildworks さん

    はじめまして、返信ありがとうございます。
    ご提示していただいた同じ記事を自分も夜中に見つけまして、divで囲ってそのdivとul、liに階層ごとのクラスをつけることはまだできていませんが、警告エラーは解消することができました。
    ご提案ありがとうございます。

    2階層(親子)まではifで実現できている記事を見つけたのですが、3階層以降の参考記事が見当たらず…phpに関してもそこまで知見がないため、引き続き調べつつ調べつつ頑張ってみようと思います。

    とりあえず、wp_nav_menu() のパラメータに関してですが、

    megamenu クラスが呼ばれていないようです。

    'walker' => new Walker_Nav_Menu(),

    'walker' => new megamenu(),

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「wp_nav_menu()で子メニュー、孫メニューをdivで囲むには」には新たに返信することはできません。