• Hi,

    I’m Jana and I’m trying to customize my wordpress menu and want to have the following result:

    <ul>
    <li id="1">
    <a id="1"></a>
    <div id="1"></div>
    </li>
    <li id="2">
    <a id="2"></a>
    <div id="2"></div>
    </li>
    <li id="3">
    <a id="3"></a>
    <div id="3"></div>
    </li>
    </ul>

    I’m using already a walker function. Please help me.

Viewing 1 replies (of 1 total)
  • Thread Starter jana26

    (@jana26)

    I tried to add snippets of this solution.

    https://wordpress.stackexchange.com/questions/90346/costum-walker-with-sub-menu-item-count

    but the problem is that the submenu div is made in another function.
    That’s my Walker:

    static $x = 0;
    
    	function start_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat("\t", $depth);
            $output .= "\n$indent<div id='p7DMMs1_2' class='p7dmm-sub-wrapper'><ul class='sub-menu'>\n";
        }
        function end_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat("\t", $depth);
            $output .= "$indent</ul></div>\n";
        }
    
    	function start_el ( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        // Most of this code is copied from original Walker_Nav_Menu
        global $wp_query, $wpdb;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
        $class_names = $value = '';
    
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = '';
    
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = ' class="menu-item"';
    
        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = ' id=""';
    
        $has_children = $wpdb->get_var("SELECT COUNT(meta_id)
                                FROM wp_postmeta
                                WHERE meta_key='_menu_item_menu_item_parent'
                                AND meta_value='".$item->ID."'");
    
        $output .= $indent . '<li' . $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        ) .'"' : '';
    	if ( $depth == 0 ) {
    		$attributes .= ' id="p7DMMt1_'.$x.'"';
    	}
        // Check if menu item is in main menu
        if ( $depth == 0 && $has_children > 0  ) {
    		$item_html = '';
            // These lines adds your custom class and attribute
            $attributes .= ' class="trig"';
            $attributes .= ! empty( $item->url )        ? ' href="#"' : '';
        } else {
    		$attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
    	}
    
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    
        // Add the caret if menu level is 0
        if ( $depth == 0 && $has_children > 0  ) {
            $item_output .= '';
        }
    
        $item_output .= '</a>';
        $item_output .= $args->after;
    
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
      }
Viewing 1 replies (of 1 total)
  • The topic ‘Walker Menu’ is closed to new replies.