WordPress菜单在标签中添加类

我正在尝试为主题创建简单的引导导航, 这里有类似这样的html代码

< ul class="navbar-nav mx-auto"> < li class="nav-item"> < a class="nav-link" href="http://www.srcmini.com/index.html"> HOME< /a> < /li> < li class="nav-item"> < a class="nav-link" href="http://www.srcmini.com/about.html"> ABOUT US< /a> < /li> < li class="nav-item"> < a class="nav-link" href="http://www.srcmini.com/contact.html"> CONTACT< /a> < /li> < /ul>

为了欲望的输出, 我写了
< ?php wp_nav_menu( array( 'container'=> false, 'menu_class'=> 'navbar-nav mx-auto' ) ); ?>

我得到的结果像
< ul class="navbar-nav mx-auto"> < li class="nav-item"> < ahref="http://www.srcmini.com/#"> HOME< /a> < /li> < li class="nav-item"> < ahref="http://www.srcmini.com/#"> ABOUT US< /a> < /li> < li class="nav-item"> < ahref="http://www.srcmini.com/#"> CONTACT< /a> < /li> < /ul>

如何在标签中添加class =” nav-link” ?
#1通过nav_menu_link_attributes很容易
function menu_anchors( $atts ) { $atts['class'] = 'nav-link'; return $atts; } add_filter( 'nav_menu_link_attributes', 'menu_anchors' );

#2【WordPress菜单在标签中添加类】请在功能文件中放置以下功能, 然后更改菜单位置:
function add_specific_menu_location_atts( $atts, $item, $args ) { // check if the item is in the primary menu if( $args-> theme_location == 'primary' ) { // add the desired attributes: $atts['class'] = 'nav-link'; } return $atts; } add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );

    推荐阅读