Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have used wp_nav_menu() for my custom WP sites with Custom Walker Function which is working fine and providing me below output:

<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 div class through loop after the ul class="col-sm-12 list-unstyled" inside the div class "yamm-content". So 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 idea how can i get it from my custom Walker function. I just tried to get it through static way but don't know how to place it after ending the </UL>. Anybody 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">';


     }

     function end_lvl(&$output, $depth) {
        $output .= '</ul></div> </li></ul>';

    }

    // add main/sub classes to li's 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

        }
    }
}
share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.