如何在菜单项上显示动态侧栏

时间:2014-04-02 19:19:37

标签: wordpress wordpress-theming

我想在菜单项上显示动态侧边栏内容(在<li></li>标记内)但不幸的是侧边栏内容显示<nav></nav>标记的顶部,我的意思是菜单顶部和我在这里调用动态边栏的位置显示结果1.我没有找到任何与此问题相关的建议。所以,任何人都可以帮助我。谢谢。这是我的菜单代码

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>

1 个答案:

答案 0 :(得分:0)

我知道问题出在哪里。

您无法在连接到add_filter的函数内调用dynamic_sidebar(&#34; mega menu&#34;)(&#39; wp_nav_menu_items&#39;,&#39; your_custom_menu_item&#39;,10 ,2);

我最近写了一个插件,几乎有同样的问题。

你需要

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.
  }
 }

这是来自http://wordpress.org/plugins/get-different-menus/的插件,可能会帮助您更好地理解菜单的工作方式!