• Hi everyone,
    I’m trying to make a navigation that has a similar structure of this website: https://grannys.ca/ But I got struck in the logic of how can I implement an if statements which determine several things:

    1- if the the link has a class of logo then add before it a container for this link.

    2- a container for the rests of links should be made and devide the links in a half and contain a container for each half.

    Here’s the prefered output:

    <ul>
            <div class="col-sm-2 col-sm-push-5 text-center">
                  <a href="index.php" id="main-nav-link" class="logo"><img src="img/image.png" width="100" height="100" alt="Logo"></a>
                  <button id="menu-trigger" class="menu-trigger visible-xs"><i class="fa fa-bars fa-2x"></i></button>
            </div>
           <div class="mob-menu-bg">
                  <div class="col-sm-5 col-sm-pull-2 left text-center">
                        <li><a href="portrait.php" id="main-nav-link">Portrait</a></li>
                        <li><a href="repertoire.php" id="main-nav-link">Repertoire</a></li>
                        <li><a href="kundenresumes.php" id="main-nav-link">Kundenresumés</a></li>
                  </div>
                  <div class="col-sm-5 col-sm-push-0 col-sm-pull-0 right text-center">
                        <li><a href="duo.php" id="main-nav-link">Duo</a></li>
                        <li><a href="referenzen.php" id="main-nav-link">Referenzen</a></li>
                        <li><a href="kontakt.php" id="main-nav-link">Kontakt</a></li>
                  </div>
          </div>
      </ul>

    and here’s the walker class that I’m trying to implement:

    class Walker_Main_Nav extends Walker_Nav_Menu {
    
      function start_lvl( &$output, $depth = 0, $args = array() )  {
            $indent = str_repeat("\t", $depth);
            $output .= "\n$indent<ul>\n";
      }
    
      function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
            $indent = ($depth) ? str_repeat("\t", $depth) : '';
            // $li_attributes = '';
            // $class_names = $value = '';
            $current_indicators = array('');
            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $classes[] = ($args->walker->has_children) ? 'dropdown' : '';
            $classes[] = ($item->current || $item->current_item_anchestor) ? 'active' : '';
            if ($depth && $args->walker->has_children) {
                  $classes[] = 'dropdown-submenu';
            }
            $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter( $classes ), $item, $args ) );
            $class_names = ' class="' . esc_attr($class_names) . '" ';
    
            $id = apply_filters('nav_menu_item_id', 'menu_item_'.$item->ID, $item, $args);
            $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
            $output .= $indent . '<li' . $id . $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 .= ( $args->walker->has_children ) ?  ' class="dropdown-toggle" data-toggle="dropdown"' : '';
            $item_output = $args->before;
            $item_output .= '<a' . $attributes . '>';
            $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
            $item_output .= ( $depth == 0 && $args->walker->has_children ) ? '<b class="caret"></b></a>' : '</a>';
            $item_output .= $args->after;
    
            $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
      }}

    Note: I’m a very beginner to wordpress custom theme development.

    Please help with this.

  • The topic ‘custom menu structure with walker class’ is closed to new replies.