如何将类名传递给wp_nav_menu中的子菜单项

时间:2017-07-10 10:57:07

标签: php wordpress

我想将类名添加到子菜单ul

wp_nav_menu(array('theme_location'=>'primary-menu','menu_id'=>'primary-menu'));

2 个答案:

答案 0 :(得分:1)

试试这个,我相信这会对你有所帮助。 wp_nav_menu过滤器应该可以帮助您这样做,或者有其他过滤器wp_nav_menu_itemswp_nav_menu_{$menu->slug}_items那些可以帮助您。有关详细信息,请访问this

function new_submenu_class($menu) {    
    $menu = preg_replace('/ class="sub-menu"/','/ class="yourclass" /',$menu);        
    return $menu;      
}

add_filter('wp_nav_menu','new_submenu_class'); 

或扩展Walker_Nav_Menu类并覆盖start_lvl()

答案 1 :(得分:0)

替换课程

    <?php 
$defaults = array(
    'theme_location'  => 'primary-menu',
    'menu'            => '',
    'container'       => '',
    'container_class' => '',
    'container_id'    => '',
    'menu_class'      => '',
    'menu_id'         => 'primary-menu',
    'echo'            => false,
    'fallback_cb'     => '',
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'depth'           => 0,
    'walker'          =>''
);
// your class name : like menu_sub
echo str_replace('sub-menu', 'menu_sub', wp_nav_menu( $defaults)
);
?>