如何使用wp_get_NAV_MENU_ITEMS函数包含第三个级别

时间:2017-09-29 作者:user1676224

我正在尝试创建一个自定义菜单结构,该结构将与jquery推送菜单一起使用。http://multi-level-push-menu.make.rs/

第一级和第二级的一切都很好。如果我尝试在菜单中添加第三级菜单,一切都会变得很奇怪。显然,这是由于该功能只占第二级。

但不幸的是,我的wordpress知识有限,不允许我再进一步。

    <?php 

function clean_custom_menu( $theme_location ) {
    if ( ($theme_location) && ($locations = get_nav_menu_locations()) && isset($locations[$theme_location]) ) {
        $menu = get_term( $locations[$theme_location], \'nav_menu\' );
        $menu_items = wp_get_nav_menu_items($menu->term_id);

        $menu_list  = \'<nav>\' ."\\n";
        $menu_list .= \'<h2><i class="fa fa-reorder"></i>Navigation</h2>\' ."\\n";
        $menu_list .= \'<ul class="main-nav">\' ."\\n";

        $count = 0;
        $submenu = false;

        foreach( $menu_items as $menu_item ) {
             //var_dump($menu_item);
            $link = $menu_item->url;
            $title = $menu_item->title;
            $classes = $menu_item->classes;
            $description = $menu_item->description;

            if ( !$menu_item->menu_item_parent ) {
                $parent_id = $menu_item->ID;

                $menu_list .= \'<li class="item">\' ."\\n";
                if (!empty($description)) {
                    $menu_list .= \'<a href="\'.$link.\'" class="title"><i class="fa fa-\'.$description.\'"></i>\'.$title.\'</a>\' ."\\n";
                    $menu_list .= \'<h2><i class="fa fa-\'.$description.\'"></i>\'.$title.\'</h2>\' ."\\n";
                } else {
                    $menu_list .= \'<a href="\'.$link.\'" class="title">\'.$title.\'</a>\' ."\\n";
                }
            }

            if ( $parent_id == $menu_item->menu_item_parent ) {

                if ( !$submenu ) {
                    $submenu = true;
                    $menu_list .= \'<ul class="sub-menu">\' ."\\n";
                }

                $menu_list .= \'<li class="item">\' ."\\n";
                if (!empty($description)) {
                    $menu_list .= \'<a href="\'.$link.\'" class="title"><i class="fa fa-\'.$description.\'"></i>\'.$title.\'</a>\' ."\\n";
                    $menu_list .= \'<h2><i class="fa fa-\'.$description.\'"></i>\'.$title.\'</h2>\' ."\\n";
                } else {
                    $menu_list .= \'<a href="\'.$link.\'" class="title">\'.$title.\'</a>\' ."\\n";
                }
                $menu_list .= \'</li>\' ."\\n";


                if ( $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ){
                    $menu_list .= \'</ul>\' ."\\n";
                    $submenu = false;
                }

            }

            if ( $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) { 
                $menu_list .= \'</li>\' ."\\n";      
                $submenu = false;
            }

            $count++;
        }

        $menu_list .= \'</ul>\' ."\\n";
        $menu_list .= \'</nav>\' ."\\n";

    } else {
        $menu_list = \'<!-- no menu defined in location "\'.$theme_location.\'" -->\';
    }
    echo $menu_list;
}
 ?>

1 个回复
最合适的回答,由SO网友:user1676224 整理而成

似乎没有人能回答这个问题,所以我按照@Zlatev的建议创建了一个自定义的walker类。。。

从GIT下载插件并将其添加到主题中,将下面的自定义walker添加到include()或直接添加到函数中。

<?php 
    /**
     * Required plugin
     * https://github.com/adgsm/multi-level-push-menu
     */
    class Push_Menu_Walker extends Walker_Nav_Menu {
        /**
         * Start the element output.
         *
         * @param  string $output Passed by reference. Used to append additional content.
         * @param  object $item   Menu item data object.
         * @param  int $depth     Depth of menu item. May be used for padding.
         * @param  array $args    Additional strings.
         * @return void
         */
        function start_el( &$output, $item, $depth = 5, $args = array(), $id = 0 ) {

            $output .= "<li>";

            $attributes  = \'\';
            $title = $item->title;
            $desc = $item->description;
            $classes = $item->classes;
                    /*["classes"] => array(8) {
                        [0]=> string(0) "" <-- THIS IS THE CUSTOM CLASS
                        [1]=> string(9) "menu-item" 
                        [2]=> string(24) "menu-item-type-post_type" 
                        [3]=> string(21) "menu-item-object-page" 
                        [4]=> string(17) "current-menu-item" 
                        [5]=> string(9) "page_item"
                        [6]=> string(12) "page-item-50"
                        [7]=> string(17) "current_page_item"
                    }*/
            $font_awesome_class = \' class="fa fa-\'. $classes[0] . \'"\';

            ! empty( $item->url )
                and $attributes .= \' href="\'   . esc_attr( $item->url        ) .\'"\';

            $title = apply_filters( \'the_title\', $item->title, $item->ID );

            if (!empty ( $classes[0] )) : // If we have a custom class, add the H2 + icon
                    $item_output = $args->before
                        . "<a $attributes>"
                        .       $args->link_before
                        .       $title
                        . \'</a> \'
                        . \'<h2>\'
                        .       \'<i \' . $font_awesome_class . \'></i>\'
                        .       $title
                        . \'</h2>\'
                        . $args->link_after
                        // . $description <-- Not needed for now...
                        . $args->after;
            else :
                    $item_output = $args->before
                        . "<a $attributes>"
                        . $args->link_before
                        . $title
                        . \'</a> \'
                        . $args->link_after
                        . $args->after;
            endif;

            // Since $output is called by reference we don\'t need to return anything.
            $output .= apply_filters(
                \'walker_nav_menu_start_el\',
                $item_output,
                $item,
                $depth,
                $args
            );
        }
    }
 ?>
最后,在主题中给步行者打电话:

更新theme\\u位置通过创建空参数移除容器调用我们刚刚创建的walker

<div id="push-nav"> <nav> <h2>Navigation <i class="fa fa-reorder"></i></h2> <?php wp_nav_menu( array( \'theme_location\' => \'menu-3\', \'container\'=>\'\', \'walker\' => new Push_Menu_Walker()) ); ?> </nav> </div><!-- /#page -->

结束

相关推荐

Problem with custom menus

在菜单上,我说的是自定义菜单,我有一个带有几列的页脚,每列都有一个项目列表,每列菜单都需要从wp admin上的菜单选项进行更新,所以我创建了关于函数的部分。调用菜单并在页脚上添加每个菜单的php。php文档,类似于:<?php wp_nav_menu( array(\'menu\' => \'f-1\', \'menu_class\' => \'\', \'container\' => \'nav\' )); ?> <?php wp_nav_menu( arr