如何仅使用wp_NAV_MENU()显示子菜单

时间:2012-06-19 作者:Nolan

如果您位于父页面,我将使用此代码显示自定义菜单子页面。但这段代码还显示了一个没有子菜单的页面上的父页面,我想隐藏它。基本上只有当我们在一个有子页的页面上时才使用此代码,

class Selective_Walker extends Walker_Nav_Menu
{
    function walk( $elements, $max_depth) {

        $args = array_slice(func_get_args(), 2);
        $output = \'\';

        if ($max_depth < -1) //invalid parameter
            return $output;

        if (empty($elements)) //nothing to walk
            return $output;

        $id_field = $this->db_fields[\'id\'];
        $parent_field = $this->db_fields[\'parent\'];

        // flat display
        if ( -1 == $max_depth ) {
            $empty_array = array();
            foreach ( $elements as $e )
                $this->display_element( $e, $empty_array, 1, 0, $args, $output );
            return $output;
        }

        /*
         * need to display in hierarchical order
         * separate elements into two buckets: top level and children elements
         * children_elements is two dimensional array, eg.
         * children_elements[10][] contains all sub-elements whose parent is 10.
         */
        $top_level_elements = array();
        $children_elements  = array();
        foreach ( $elements as $e) {
            if ( 0 == $e->$parent_field )
                $top_level_elements[] = $e;
            else
                $children_elements[ $e->$parent_field ][] = $e;
        }

        /*
         * when none of the elements is top level
         * assume the first one must be root of the sub elements
         */
        if ( empty($top_level_elements) ) {

            $first = array_slice( $elements, 0, 1 );
            $root = $first[0];

            $top_level_elements = array();
            $children_elements  = array();
            foreach ( $elements as $e) {
                if ( $root->$parent_field == $e->$parent_field )
                    $top_level_elements[] = $e;
                else
                    $children_elements[ $e->$parent_field ][] = $e;
            }
        }

        $current_element_markers = array( \'current-menu-item\', \'current-menu-parent\', \'current-menu-ancestor\' );  //added by continent7
        foreach ( $top_level_elements as $e ){  //changed by continent7
            // descend only on current tree
            $descend_test = array_intersect( $current_element_markers, $e->classes );
            if ( !empty( $descend_test ) ) 
                $this->display_element( $e, $children_elements, 2, 0, $args, $output );
        }

        /*
         * if we are displaying all levels, and remaining children_elements is not empty,
         * then we got orphans, which should be displayed regardless
         */
         /* removed by continent7
        if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
            $empty_array = array();
            foreach ( $children_elements as $orphans )
                foreach( $orphans as $op )
                    $this->display_element( $op, $empty_array, 1, 0, $args, $output );
         }
        */
         return $output;
    }
}
在模板中使用:

wp_nav_menu( 
   array(
       \'theme_location\'=>\'primary\', 
       \'walker\'=>new Selective_Walker() 
   )
);

2 个回复
SO网友:Matt Keys

在过去的Wordpress中,我也曾多次遇到过同样的问题。在其他CMS中,我使用的菜单总是有一个start\\u深度选项,这使得实现二级或三级菜单(分割菜单)非常容易。

Wordpress不包含此功能;而且许多在线解决方案使用wp\\U list\\U页面,完全忽略了菜单管理器层次结构。

在尝试了不同的walker和widget之后,我最终决定编写自己的插件,其中包含start\\u depth选项。

您可以这样使用它:

wp_nav_menu(array(\'theme_location\' => \'primary_navigation\', \'start_depth\' => 1));
任何感兴趣的人都可以在WordPress插件库中免费获得:https://wordpress.org/plugins/wp-nav-plus/

SO网友:Junaid Bhura

我已经做了一个免费的插件来解决这个问题!

https://wordpress.org/plugins/wp-nav-menu-extended/

此插件扩展了本机wp\\u nav\\u菜单功能,并添加了其他选项:

  • level : (整数)(此插件工作所需)要显示的导航菜单级别。如果未传递参数的子项,则显示该级别的所有项目child_of : (字符串|整数)(可选)菜单中要显示其直接子菜单的父菜单的标题或菜单项ID

    Sample Usage:

    $defaults = array( \'theme_location\' => \'main_menu\', \'level\' => 2, \'child_of\' => \'About Us\', );
    
    wp_nav_menu( $defaults );