Wordpress教程 2023年04月5日
0 收藏 0 点赞 1,128 浏览 2195 个字

先注册菜单位置,这里我自定义:flmenu

// 注册菜单位置
if (function_exists('register_nav_menus')) {
    register_nav_menus(array(
        'flmenu' => __('分类侧边栏菜单'),
    ));
}

class MyWalker extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("\t", $depth);
        $sub_menu_class = '';
        switch ($depth) {
            case 0:
                $sub_menu_class .= 'aa'; //根据深度自定每级的class类名
                break;
            case 1:
                $sub_menu_class .= 'bb'; //根据深度自定每级的class类名
                break;
            case 2:
                $sub_menu_class .= 'cc'; //根据深度自定每级的class类名
                break;
            // Add more cases for deeper levels as needed
        }
        $output .= "\n$indent<ul class=\"$sub_menu_class\">\n";
    }

    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
        $indent = ($depth) ? str_repeat("\t", $depth) : '';
        $class_names = $value = '';
        $classes = empty($item->classes) ? array() : (array) $item->classes;

        // 添加菜单项的深度类名
        if ($depth > 0) {
            $depth_class = 'depth-' . ($depth + 1);
            $classes[] = $depth_class;
            switch ($depth) {
                case 1:
                    $classes[] = 'aa' . ($depth + 1); //根据深度自定每级的class类名
                    break;
                case 2:
                    $classes[] = 'bb' . ($depth + 1); //根据深度自定每级的class类名
                    break;
                case 3:
                    $classes[] = 'cc' . ($depth + 1); //根据深度自定每级的class类名
                    break;
                // Add more cases for deeper levels as needed
            }
        }
        
        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
        $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
        $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
        $id = $id ? ' id="' . esc_attr($id) . '"' : '';
        $output .= $indent . '<li' . $id . $value . $class_names . '>';
        $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '" rel="external nofollow" ' : '';
        $item_output = $args->before;
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;
        $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
    }

    function end_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }
}

 

前台调用

// 输出菜单
wp_nav_menu(array(
    'theme_location' => 'flmenu',
   'menu_class' => 'my-custom-class',
    'container_class' => 'my-menu-container',
    'walker' => new MyWalker()
));

 

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/2061.html

相关推荐
WordPress去除/禁止后台侧栏插件-主题-版本更新提示
今天使用了张戈修改的nginx-help插件修改版,官方版并无自动重启功能,因此不能更新。但是这WordPress后台一直有更新提示,这可真…
日期:2024-12-30 点赞:0 阅读:48
WordPress网站js防扒代码-禁止右键/F12/调试自动关闭窗口
网站防扒老生常谈了,今天老白博客@老白也给大家分享一下从网上看到的“WordPress网站防扒代码/禁止F12调试教程-以7b2主题为例” …
日期:2024-12-30 点赞:0 阅读:62
WordPress优化加速之导航菜单栏缓存Menu Cache
今天老白博客@老白给大家分享又一个WordPress优化加速教程:“导航菜单栏缓存WordPress插件Menu Cache”。我们都知道数…
日期:2024-12-30 点赞:0 阅读:48
2024基于网站性能测试的WordPress优化指南
老白博客WordPress优化高级教程:在如今5G遍地走,千兆多如狗的高速网络时代,如果你访问网站,还出现转圈圈的情况,那么你十有八九是要关…
日期:2024-12-30 点赞:0 阅读:53
WP网站用户行为监测回放插件nicen-replay
在数字营销的世界里,了解用户行为是提升用户体验和转化率的关键。老白推荐一款炫酷的WordPress插件:nicen-replay 。它能够让…
日期:2024-12-30 点赞:0 阅读:51
2024新WordPress缓存插件atec Cache APCu
WordPress插件atec Cache APCu 是一款2024年才投入使用的的高效缓存插件,与其他内存缓存选项(如 Redis 和 M…
日期:2024-12-30 点赞:0 阅读:70
发表评论
暂无评论

还没有评论呢,快来抢沙发~