1

現在、WordPress カスタム ウォーカーを使用してナビゲーションをカスタマイズしています。現在、このナビゲーションを生成します:

<nav class="nav-top clearfix">
<ul class="clearfix">
<li id="menu-item-69" class="current_page_item"><a href="#">test1</a></li>
<li id="menu-item-68"><a href="#">test2</a></li>
<li id="menu-item-67"><a href="#">test3</a></li>
<li id="menu-item-66"><a href="#">test4</a></li>
<li id="menu-item-65"><a href="#">test5</a></li>
<li id="menu-item-64"><a href="#">test6</a></li>
</ul>
</nav>

test2 をクリックすると、current_page_item クラスが適用されます。

すべてが正常に機能していますが、フッターでも同じナビゲーションを使用したいと考えています。それを行うと、li id が原因で検証エラーが発生します。li id を li クラスに変更するにはどうすればよいですか?

これが私のカスタム ウォーカーです。

class My_Walker extends Walker_Nav_Menu {
  function start_el(&$output, $item, $depth, $args)
  {
       global $wp_query;
       $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

       $class_names = $value = '';

       $classes = empty( $item->classes ) ? array() : (array) $item->classes;

       $current_indicators = array('current_page_item');

       $newClasses = array();

       foreach($classes as $el){
           //check if it's indicating the current page, otherwise we don't need the class
           if (in_array($el, $current_indicators)){ 
               array_push($newClasses, $el);
           }
       }

       $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $newClasses), $item ) );
       if($class_names!='') $class_names = ' class="'. esc_attr( $class_names ) . '"';


       $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $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        ) .'"' : '';

       if($depth != 0)
       {
           //children stuff, maybe you'd like to store the submenu's somewhere?
       }

       $item_output = $args->before;
       $item_output .= '<a'. $attributes .'>';
       $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
       $item_output .= '</a>';
       $item_output .= $args->after;

       $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
       }
}
4

1 に答える 1

0

まだそれを理解していない場合は、次のことを行う必要があります。

次のように置き換えます$newClasses = array();

$newClasses = array( 'menu-item-'. $item->ID );

次に、次の行を置き換えます。

$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

と:

$output .= $indent . '<li' . $value . $class_names .'>';
于 2012-11-23T11:17:20.763 に答える