如何在WordPress的菜单项中添加自定义类。下面是我的代码,它不起作用

header.php

/*Render menu*/ < div class="main-nav"> < ?php wp_nav_menu( array( 'theme_location' => 'header-menu', 'container' => 'ul', 'menu_class' => 'nav' ) ); ?> < /div>

functions.php
/*Register menu*/ function register_main_menu(){ register_nav_menus(array( 'Primary' => __('Header Menu'), 'Footer' => __('Footer Menu') )); } add_action('after_setup_theme', 'register_main_menu'); /*Custom menu class*/ function add_class_to_li($classes, $item){ $classes[] = "nav-item"; return $classes; } add_filter('nav_menu_css_class', 'add_class_to_li', 10, 4);

这不会在” li” 中添加” nav-item” 类。有什么我需要更新的吗?
#1【如何在WordPress的菜单项中添加自定义类。下面是我的代码,它不起作用】你的theme_location应该与在functions.php文件中拥有的theme_location完全一样。因此, 你的阅读器菜单将是:
/*Render menu*/ < div class="main-nav"> < ?php wp_nav_menu( array( 'theme_location' => 'Primary', //this will be Primary, not header-menu 'container' => 'ul', 'menu_class' => 'nav' ) ); ?> < /div>

    推荐阅读