为WordPress导航菜单添加子菜单专属图标问题求助
解决WordPress导航菜单仅给含子菜单的项添加图标的问题
问题原因
你直接使用$menu_item->has_children的判断不生效,是因为WordPress传递给start_el方法的WP_Post对象(即$menu_item)默认不会自动填充has_children属性。这个属性并非菜单项本身的固有属性,而是Walker类在构建菜单树结构时内部使用的标记,无法直接通过$menu_item访问。
解决方案
1. 避免修改核心文件(重要)
不要直接编辑class-walker-nav-menu.php核心文件,WordPress更新会覆盖你的修改。正确做法是在主题的functions.php中创建自定义Walker类,继承默认的Walker_Nav_Menu。
2. 自定义Walker类实现条件判断
通过查询当前菜单的所有项,检查是否存在以当前项为父级的子项,替代$menu_item->has_children的判断。以下是完整的自定义Walker类代码:
class Custom_Nav_Walker extends Walker_Nav_Menu { public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) { $menu_item = $data_object; if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { $t = ''; $n = ''; } else { $t = " "; $n = " "; } $indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; $classes = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes; $classes[] = 'menu-item-' . $menu_item->ID; $args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth ); $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) ); $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth ); $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; $output .= $indent . '<li' . $id . $class_names . '>'; $atts = array(); $atts['title'] = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : ''; $atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : ''; if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) { $atts['rel'] = 'noopener'; } else { $atts['rel'] = $menu_item->xfn; } $atts['href'] = ! empty( $menu_item->url ) ? $menu_item->url : ''; $atts['aria-current'] = $menu_item->current ? 'page' : ''; $atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth ); $attributes = ''; foreach ( $atts as $attr => $value ) { if ( is_scalar( $value ) && '' !== $value && false !== $value ) { $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); $attributes .= ' ' . $attr . '="' . $value . '"'; } } $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth ); // 自定义判断:检查当前菜单项是否有子项 $has_children = $this->has_children( $menu_item, $args ); $item_output = $args->before; $item_output .= $has_children ? '<i class="icon1"></i>' : ''; $item_output .= '<a' . $attributes . '>'; $item_output .= $args->link_before . $title . $args->link_after; $item_output .= '</a>'; $item_output .= $has_children ? '<i class="icon2"></i>' : ''; $item_output .= $args->after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args ); } // 新增辅助方法:检查菜单项是否有子项 private function has_children( $menu_item, $args ) { if ( empty( $args->menu ) ) { return false; } // 获取当前菜单的所有项 $menu_items = wp_get_nav_menu_items( $args->menu ); foreach ( $menu_items as $item ) { if ( $item->menu_item_parent == $menu_item->ID ) { return true; } } return false; } }
3. 调用自定义Walker类
在调用wp_nav_menu时,指定使用自定义的Walker类:
wp_nav_menu( array( 'theme_location' => 'primary', // 替换为你的菜单位置 'walker' => new Custom_Nav_Walker() ) );
高效替代方案
如果觉得每次查询菜单项效率不高,可以提前在Walker的构造函数中预存所有子项的父ID映射:
class Custom_Nav_Walker extends Walker_Nav_Menu { private $child_parent_map = array(); public function __construct( $menu_id = '' ) { if ( $menu_id ) { $menu_items = wp_get_nav_menu_items( $menu_id ); foreach ( $menu_items as $item ) { if ( $item->menu_item_parent ) { $this->child_parent_map[ $item->menu_item_parent ] = true; } } } } public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) { // ... 其他代码保持不变 ... // 使用预存的映射判断是否有子项 $has_children = isset( $this->child_parent_map[ $menu_item->ID ] ); // ... 后续代码不变 ... } }
调用时传入菜单ID:
wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new Custom_Nav_Walker( 'primary-menu' ) // 替换为你的菜单ID或slug ) );
内容的提问来源于stack exchange,提问作者Pawe Eleno
相关产品推荐
相关产品推荐

