正在尝试存储要在主菜单之后呈现的子菜单项

时间:2015-01-23 作者:emily

我正在使用一个自定义导航漫游器来呈现主菜单,在此期间,我想将第二级导航项存储在页面的一个单独区域中,以便稍后使用/呈现。

我希望这些二级导航项目显示块,如果您当前在该部分(即,如果您在“服务”或“服务”的子页面上,则子菜单将显示块)。此外,如果我将鼠标悬停在“服务”上,我希望显示子菜单。

目前,我已经在一个单独的自定义导航步行器中完成了子菜单,但导航仅在您位于该部分时显示,而不是在您将鼠标悬停在主菜单项上时显示。此外,根据当前的设置方式,它会在没有子菜单的页面上显示菜单背景/样式

这是我现有的导航助行器,仅用于子菜单:

class MainSubmenuCustomWalker extends \\Walker_Nav_Menu
{
    function start_lvl(&$output, $depth=0, $args=array())
    {
        if ($depth == 0) {
            return;
        }
        parent::start_lvl($output, $depth, $args);
    }

    function end_lvl(&$output, $depth=0, $args=array())
    {
        if ($depth == 0) {
            return;
        }
        parent::end_lvl($output, $depth,$args);
    }

    // Don\'t print top-level elements
    public function start_el(&$output, $item, $depth=0, $args=array(), $id=0)
    {
        if ($depth == 0) {
            return;
        }
        parent::start_el($output, $item, $depth, $args);
    }

    function end_el(&$output, $item, $depth=0, $args=array())
    {
        if ($depth == 0) {
            return;
        }
        parent::end_el($output, $item, $depth, $args);
    }

    function display_element($element, &$children_elements, $max_depth, $depth, $args, &$output)
    {
        $current_element_markers = array( \'current-menu-item\', \'current-menu-parent\', \'current-menu-ancestor\' );
        $current_class = array_intersect( $current_element_markers, $element->classes );
        $ancestor_of_current = !empty($current_class);

        if (0 == $depth && !$ancestor_of_current) {
            return;
        }

        $id = $element->ID;
        if (($max_depth == 0 || $max_depth > $depth+1) && isset($children_elements[$id])) {
            foreach ($children_elements[$id] as $child) {
                if (!isset($newlevel)) {
                    $newlevel = true;
                    //start the child delimiter
                    $cb_args = array_merge(array(&$output, $depth), $args);
                    call_user_func_array(array($this, \'start_lvl\'), $cb_args);
                }
                parent::display_element($child, $children_elements, $max_depth, $depth + 1, $args, $output);
            }
            unset($children_elements[$id]);
        }

        if (isset($newlevel) && $newlevel) {
            //end the child delimiter
            $cb_args = array_merge(array(&$output, $depth), $args);
            call_user_func_array(array(&$this, \'end_lvl\'), $cb_args);
        }
    }
}
从此处调用,即使没有子菜单项,也会呈现菜单容器:(

<div class="main-submenu-navbar navbar navbar-default">
<div class="container">
    <div class="navbar-header">
        <button type="button"
        class="navbar-toggle collapsed"
        data-toggle="collapse"
        data-target="#main-submenu-collapse">
        <span class="sr-only">Toggle Navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
</div>
<?php Menu::submenus(); ?>

1 个回复
SO网友:Tobias Beuving

昨天我遇到了同样的问题,我决定放弃walker 一起使用wp_get_nav_menu_objectwp_get_nav_menu_items 而不是函数。

如果你var_dump($menu_items) 您可以查看哪些属性可用。

下面是我的代码片段,它为我的twitter引导菜单构建了名为“main”的菜单:

    $menu_name = \'main\'; // name of your menu
    $menu = wp_get_nav_menu_object($menu_name);

    $menu_items = wp_get_nav_menu_items($menu->term_id);



    $tmpWPMenu = ""; // string variable where the menu is stored

    // collect the menu-items (if any) and store them temporarily in 
    // the $tmpChildren array with the $key = id of parent

    $tmpChildren = array();
    foreach ( (array) $menu_items as $key => $menu_item ) {
        if (intval($menu_item->menu_item_parent) > 0) {
            $tmpChildren[$menu_item->menu_item_parent][] = $menu_item;
        }
    }

   // Now loop again and build it, 
   // when a matching ID is encountered in the $tmpChildren array, build the submenu:
    foreach ( (array) $menu_items as $key => $menu_item ) {
        $title = $menu_item->title;
        $url = $menu_item->url;

        if (intval($menu_item->menu_item_parent) == 0) {


            // Does $tmpChildren contain a $key that is the id of this menu-item?
            if (array_key_exists($menu_item->ID, $tmpChildren)) {

                $tmpWPMenu .= \'<li class="dropdown">\';
                $tmpWPMenu .= \' <a href="#" class="dropdown-toggle" data-toggle="dropdown"></i> \'.$title.\' <b class="caret"></b></a>\';


                $tmpWPMenu .= \' <ul class="dropdown-menu">\';
                foreach ($tmpChildren[$menu_item->ID] as $subMenuItem) {
                    $tmpWPMenu .= \'<li><a href="/ik/"><!--<i class="glyphicon glyphicon-user"></i>--> \' . $subMenuItem->title . \'</a></li>\';
                }
                $tmpWPMenu .= \' </ul>\';
                $tmpWPMenu .= \'</li>\';

            } else {

                $tmpWPMenu .= \'<li>\';
                $tmpWPMenu .= \'<a href="#"></i> \'.$title.\' </a>\';
                $tmpWPMenu .= \'</li>\';

            }

        }



    }

    // Do something with the $tmpWPMenu, in my case I pass it 
    // to the Smarty template engine that I use
    $tmpNavBarFindAndReplace[\'wp_menu\'] = $tmpWPMenu;


    print $gBSOManager->pPageO->mRender($tmpNavBarFindAndReplace, \'mijn/navbar.html\',true);
我希望这可能会有帮助(或提供见解):)干杯,T

结束

相关推荐

在page.php中创建的变量在header.php中为空

好的,我在page中实例化了一个类。php。那我就给你打电话get_header() 包括页眉。php现在在页眉中。php我需要访问这个类对象并执行它的一个方法,但我遇到了以下错误:\\N致命错误:未捕获异常“BadMethodCallException”,消息为“调用非对象上的成员函数links()(NULL)”我应该使用require 而不是get_header() ?