How to show dynamic side bar on menu item

I want to show dynamic sidebar content on menu item (within <li></li> tag) but unfortunately sidebar content show top of <nav></nav> tag, i mean top of the menu and where i call dynamic side bar here show result 1. I don’t find any suggestion related this problem. So please, any one can help me. Thank you. here is my menu code

1. Menu code:   

                     <nav>
                        <?php 
                        if(has_nav_menu('primary-menu')){
                          wp_nav_menu( array('menu' => 'primary-menu' ));
                        } else {
                            $pages = wp_list_pages('sort_column=menu_order&title_li=&echo=0');
                            $pages = str_replace(array('<li class="page_item">', '</li>'), '', $pages);
                            echo "<ul>".$pages."<li><a href='#'></a></li></ul>";
                        } ?>
                        </nav>

2. dynamic side bar adding code:

    add_filter('wp_nav_menu_items', 'your_custom_menu_item', 10, 2);

    function your_custom_menu_item($items, $args) {
        if ($args->menu == 'primary-menu') {
            return $items .= '

    <li><a href="#">What is blank</a>
    '.dynamic_sidebar('Mega Menu').'
    </li>
    <li><a href="#" class="icon_menu fi-shopping-cart"></a></li>
    <li><a href="#" id="pm_search" class="icon_menu fi-magnifying-glass"></a>
        <div id="searc_box">
            <div id="searc_wrapper">
                <form role="search" method="get" class="search-form" action="'.home_url( '/' ).'">
                    <input type="search" class="search-field" placeholder="Search …" value="" name="s"/>
                    <input type="submit" class="search-submit postfix" value="Search" />
                </form>
            </div>

        </div>
    </li>';
        }
        return $items;
    }

3. Result code:

<li><a href="#">What is blank</a>1</li>

Related posts

Leave a Reply