The custom Walker function needs to be configured in a certain way for wp_nav_menu in Wordpress

I used wp_nav_menu () for my custom WP sites with a custom Walker function that works fine and provides me with below result:

    <div id="navbar" class="navbar-collapse">
        <ul class="nav">    
            <li class="dropdown"><a href="#" class="dropdown-toggle">HOME</a>
                <ul class="dropdown-menu">
                    <li>
                        <div class="yamm-content">
                            <ul class="col-sm-12 list-unstyled">
                                <li><a href="#">MY Review Post 1</a></li>
                                <li><a href="#">My Test Post</a></li>
                            </ul>
                        </div>
                    </li>
                </ul>
            </li>
        </ul>
    </div>

But right now I want to add a div loop through the loop after ul class = "col-sm-12 list-unstyled" inside the yamm-content div class. Thus, the output will be:

<div id="navbar" class="navbar-collapse">
        <ul class="nav">    
            <li class="dropdown"><a href="#" class="dropdown-toggle">HOME</a>
                <ul class="dropdown-menu">
                    <li>
                        <div class="yamm-content">
                            <ul class="col-sm-12 list-unstyled">
                                <li><a href="#">MY Review Post 1</a></li>
                                <li><a href="#">My Test Post</a></li>
                            </ul>
                            <!--------- NEW DIV WILL ADD HERE ----->
                            <div class="imgmenu">
                                <img src="$imageUrl" alt="" />
                                <div class="info">
                                    <h2>title text come here</h2>
                                    <a href="#">learn more</a>
                                </div>
                            </div>

                            <!-------- END NEW DIV------>
                        </div>
                    </li>
                </ul>
            </li>
        </ul>
    </div>

Please provide some insight into how I can get it from my custom Walker feature. I was just trying to get it in a static way, but don't know how to post it after graduation </UL>. Someone please help me ..

class custom_menu_walker extends Walker_Nav_Menu {
private $curItem;

function start_lvl(&$output, $depth) {
    $thisItem = $this->curItem;

    $output .= '<ul class="sub-menu"><li>
                           <div class="yamm-content">';
            $output .= ( $depth == 0 && $thisItem->object == 'category' ) ? '<ul class="col-sm-4 list-unstyled">' : '<ul class="col-sm-12 list-unstyled">';


}

//end of the sub menu wrap
function end_lvl(&$output, $depth) {
        $output .= '</ul></div> </li></ul>';

}

    // add main/sub classes to li and links
     function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
         $this->curItem = $item;

        $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent


        // depth dependent classes
        $depth_classes = array(
            ( $depth == 0 ? 'dropdown' : '' ),
            (( $depth == 0 && $item->object == 'category' ) ? 'yamm-first' : ''),
            ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
            'menu-item-depth-' . $depth
        );
        $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 ) ) );

        // build html

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

        // link attributes
        $attributes = ' class="menu-link ' . ( $depth == 0 ? 'dropdown-toggle' : '' ) . '"';


        $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
        );


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



    function end_el( &$output, $item, $depth = 0, $args = array(), $current_object_id = 0 ) {
        if ( $depth == 0 && $item->object == 'category' ) { 

            ?>
                    <div class="imgmenu">
                            <div class="info">
                                <h2>title text come here</h2>
                                <a href="#">learn more</a>
                            </div>
                      </div>
                <?php

        }
    }
}
+3
source share
1

Walker_Nav_Menu , , WordPress, .

1) start_lvl, , , ul div class yamm-content

2) start_el li a,

3) end_el end li , , ​​( )

4) end_lvl , div <div class="imgmenu">, :

class Test_Walker extends Walker_Nav_Menu {
    private $curItem;

    function start_lvl(&$output, $depth) {
        $thisItem = $this->curItem;

        $output .= '<ul class="sub-menu"><li>
                               <div class="yamm-content">';
                $output .= ( $depth == 0 && $thisItem->object == 'category' ) ? '<ul class="col-sm-4 list-unstyled">' : '<ul class="col-sm-12 list-unstyled">';


   }

    //end of the sub menu wrap
    function end_lvl(&$output, $depth) {
            $output .= '</ul>

            <!--------- NEW DIV WILL ADD HERE ----->
                                <div class="imgmenu">
                                    <img src="$imageUrl" alt="" />
                                    <div class="info">
                                        <h2>title text come here</h2>
                                        <a href="#">learn more</a>
                                    </div>
                                </div>

                                <!-------- END NEW DIV------>


            </div> </li></ul>';

    }

    // add main/sub classes to li and links
     function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
         $this->curItem = $item;

        $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent


        // depth dependent classes
        $depth_classes = array(
            ( $depth == 0 ? 'dropdown' : '' ),
            (( $depth == 0 && $item->object == 'category' ) ? 'yamm-first' : ''),
            ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
            'menu-item-depth-' . $depth
        );
        $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 ) ) );

        // build html

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

        // link attributes
        $attributes = ' class="menu-link ' . ( $depth == 0 ? 'dropdown-toggle' : '' ) . '"';


        $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
        );


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



    /*function end_el( &$output, $item, $depth = 0, $args = array(), $current_object_id = 0 ) {
        if ( $depth == 0 && $item->object == 'category' ) { 

            ?>
                    <div class="imgmenu">
                            <div class="info">
                                <h2>title text come here</h2>
                                <a href="#">learn more</a>
                            </div>
                      </div>
                <?php

        }
    }*/
    function end_el( &$output, $item, $depth = 0, $args = array() ) {
        $output .= "</li>\n";
    }
}
+2

All Articles