导航菜单快速预览不能使用wp_get_NAV_Menu_Items!

时间:2017-09-21 作者:hesham shawky

我有一个问题,我知道为什么,但我找不到解决方案,也找不到解决方法!

问题是在菜单中使用自定义预览时,实时预览编辑不起作用,这是因为我没有使用wp_nav_menu 函数,而我使用wp_get_nav_menu_items 因为它更具自定义性,我需要按名称为每个菜单项插入不同的图标,如下面的代码所示:

// Get the nav menu based on $menu_name (same as \'theme_location\' or \'menu\' arg to wp_nav_menu)
            // This code based on wp_nav_menu\'s code to get Menu ID from menu slug
            if ( is_front_page() )
                $menu_name = \'home\';
            else
                $menu_name = \'primary\';

            if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {

                $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

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

                $menu_list = \'<ul class="menu nav list-unstyled nav-stacked" role="tablist" id="menu-nav-menu">\';


                foreach ( (array) $menu_items as $key => $menu_item ) {
                    $title = $menu_item->title;
                    $id    = $menu_item->ID;
                    $title = strtolower($title);
                    $title = rtrim($title);

                    $url = $menu_item->url;
                    $active = \'\';
                    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
                    if($url == $actual_link)
                    {
                        $active = \'active\';
                    }
                    else {
                        $active = \'\';
                    }

                    switch ($title) {
                        case \'profile\':
                            $nav_icon = \'fa-user\';
                            break;

                        case \'resume\':
                            $nav_icon = \'fa-file\';
                            break;

                        case \'portfolio\':
                            $nav_icon = \'fa-paint-brush\';
                            break;

                        case \'plans\':
                            $nav_icon = \'fa-map-signs\';
                            break;

                        case \'blog\':
                            $nav_icon = \'fa-pencil\';
                            break;

                        case \'testimonials\':
                            $nav_icon = \'fa-quote-left\';
                            break;

                        case \'clients\':
                            $nav_icon = \'fa-users\';
                            break;

                        case \'contact\':
                            $nav_icon = \'fa-envelope\';
                            break;

                        default:
                            $nav_icon = \'fa-eercast\';
                            break;
                    }

                    $menu_list .= "<li id=\'menu-item-{$id}\' class=\'{$active} menu-item menu-item-type-custom menu-item-object-custom menu-item-{$id}\'><a href=\'{$url}\'><i class=\'fa {$nav_icon}\'></i> {$title}</a></li>";
                }
                $menu_list .= \'<li><form method="get" action="\'.site_url(\'/\').\'"><input type="search" name="s" class="form-control" placeholder="Search..."></form></li>\';
                $menu_list .= \'</ul>\';

            } else {
                $menu_list = \'<ul class="nav list-unstyled nav-stacked"><li style="color: #fff">Menu "\' . $menu_name . \'" <strong>empty!</strong>.</li></ul>\';
            }

            // $menu_list now ready to output
            echo $menu_list;
因此,如果中间有任何解决方案,如我可以插入图标(仅使用代码,因为我不能使用插件),并启用快速预览定制器,我将不胜感激。

1 个回复
SO网友:Weston Ruter

您必须使用wp_nav_menu() 如果要获得选择性刷新(快速预览)。如果您希望有一个使用选择性刷新的自定义导航菜单,则不需要手动迭代wp_get_nav_menu_items() 相反,您需要将Walker_Nav_Menu 实现自定义逻辑,然后将其传递到wp_nav_menu() 像这样:

wp_nav_menu( array(
    \'theme_location\' => \'primary\',
    \'walker\' => \'My_Custom_Nav_Menu_Walker\',
) );
以下是创建自定义助行器的一个示例:https://wordpress.stackexchange.com/a/116719/8521

请注意,您必须将walker类作为字符串而不是对象传递,以便选择性刷新可用。看见full conditions.

结束

相关推荐

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