1

I want to use wordpress walker menu to achieve exactly this menu structure:

<a href="#myid" class="list-group-item" data-toggle="collapse">Item 1</a>
    <div class="collapse" id="myid">
      <a href="" class="list-group-item">Subitem 1</a>
      <a href="" class="list-group-item">Subitem 2</a>
      <a href="" class="list-group-item">Subitem 3</a>
    </div>

Here is my needs:
1- every parent menu item should have an attribute of data-toggle="collapse" and a class of list-group-item
2- all the anchor elements should have a class of list-group-item
3- all the children of parent menu item should be wrapped inside a div with the class of collapse and the id of this div should be equal to the href attribute of parent menu item. for example: parent menu has "a href='#myid' " so the container div for children of this parent menu should have an id of myid

I'm not good at working with menu walker class so could you please help me with that?

I tried some more and now I have something but not complete. could you help me with that? thank you.

<?php
class wp_bt_navwalker extends Walker_Nav_Menu {

  public function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat( "\t", $depth );
    $output .= "\n$indent<ul id=\"364\" class=\"collapse\">\n";
  }


  public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    $atts = array();
      $atts['title']  = ! empty( $item->title ) ? $item->title  : '';
      $atts['target'] = ! empty( $item->target )  ? $item->target : '';
      $atts['rel']    = ! empty( $item->xfn )   ? $item->xfn  : '';

      // If item has_children add atts to a.
      if ( $args->has_children && $depth === 0 ) {
        $atts['href']       = '#'.$item->ID;
        $atts['data-toggle']  = 'collapse';
        $atts['class']      = 'list-group-item';
      } else {
        $atts['href'] = ! empty( $item->url ) ? $item->url : '';
      }
  • any suggestion? – pershianix Jun 11 '15 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.