質問

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>
役に立ちましたか?

解決

I see where the problem is.

you can't call dynamic_sidebar("mega menu") inside a function which is connected to add_filter('wp_nav_menu_items', 'your_custom_menu_item', 10, 2);

I recently wrote a Plugin having nearly the same issue.

You need to

function your_custom_menu_item($items, $args) {



if ($args->theme_location == 'primary') {

    $items2=$items; //save $items to different variable
    $items='';//empty items to latter fill them

    // here you can write whatever you want to add
    // in the style $items. = "<li>add something</li>"

    foreach ($items2 as $item){

    $link_page_id = (int)($item->object_id);


if ($current_id == $link_page_id){$cur=" current-menu-item current_page_item";}else{$cur="";
} 

    $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page'.$cur.'"><a  href="'.$item->url.'">'.$item->title.'</a></li>';

    }

    // write something here
    // same like above

 }else{
   // what should happen if it is not primary? maybe the same without custom code.
  }
 }

The plugin where this is from is here http://wordpress.org/plugins/get-different-menus/ maybe playing around with it could help you to better understand how the menus work!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top