You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建Walker类美化WordPress主菜单出现致命错误,如何修复?

问题描述

尝试创建Walker类美化WordPress网站顶部主菜单,参考相关教程部署代码后,网站出现致命错误(提示:此网站发生严重错误,请查看网站管理员邮箱获取指导,了解更多WordPress故障排除信息)。

用到的代码

functions.php

require get_template_directory() . '/inc/walker.php';

walker.php

<?php
/* Collection of Walker classes */

class MeterInsight_Menu_Walker extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth=0, $args=null) { //ul
        $indent = str_repeat("\t",$depth);
        $submenu = ($depth > 0) ? ' sub-menu' : '';
        $output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
    }

    function start_el(&$output, $item, $depth=0, $args=[], $id=0) {
    }

    function end_el() {
    }

    function end_lvl() {
    }
}
?>
<?php
wp_nav_menu(
    array(
        'theme_location' => 'primary',
        'menu_class' => 'navbar-nav ms-auto',
        'container' => 'nav',
        'container_class' => 'collapse navbar-collapse',
        'menu' => 'primary',
        'walker' => new MeterInsight_Menu_Walker()
    )
);
?>
解决方法

一、获取详细错误信息

WordPress默认会把详细错误日志发送到管理员邮箱,也可以手动开启调试模式查看:

  1. 找到网站根目录的wp-config.php文件
  2. 修改以下代码:
    // 将WP_DEBUG改为true
    define( 'WP_DEBUG', true );
    // 开启错误日志记录(可选)
    define( 'WP_DEBUG_LOG', true );
    // 禁止前端显示错误(可选,避免影响访客)
    define( 'WP_DEBUG_DISPLAY', false );
    

开启后,错误会记录到wp-content/debug.log文件中,可查看具体错误原因。

二、修复Walker类代码问题

你的Walker类存在两个核心问题:end_el和end_lvl方法未遵循父类参数定义,start_el为空实现导致菜单渲染失败。修改后的walker.php代码如下:

<?php
/* Collection of Walker classes */

class MeterInsight_Menu_Walker extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth=0, $args=null) { //ul
        $indent = str_repeat("\t",$depth);
        $submenu = ($depth > 0) ? ' sub-menu' : '';
        $output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
    }

    function start_el(&$output, $item, $depth=0, $args=[], $id=0) {
        $indent = ($depth) ? str_repeat("\t", $depth) : '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $class_names .'>';

        $atts = array();
        $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
        $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
        $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
        $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            if ( ! empty( $value ) ) {
                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= ' ' . $attr . '="' . $value . '"';
            }
        }

        $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_el(&$output, $item, $depth=0, $args=null) {
        $output .= "</li>\n";
    }

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

修改说明

  • 补全start_el方法实现:负责渲染菜单项的<li>和<a>标签,保留WordPress默认的菜单属性过滤与处理逻辑,确保菜单功能正常
  • 修正end_el和end_lvl方法:参数定义与父类Walker_Nav_Menu保持一致,并添加正确的标签闭合逻辑

内容的提问来源于stack exchange,提问作者Erwin1441

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 05:23:28